Вот мой код:
CREATE TABLE IF NOT EXISTS Artfacture
(
N int(6) unsigned NOT NULL,
Code varchar(50) NOT NULL,
Ht Numeric NOT NULL
)
INSERT INTO Artfacture (N, Code, Ht)
VALUES ('1', '1', '10'), ('1', '2', '20'),('1', '3', '30');
CREATE TABLE IF NOT EXISTS Facture
(
N int(6) unsigned NOT NULL,
Ht Numeric NOT NULL
)
INSERT INTO Facture (N, Ht)
VALUES ('1', '60');
CREATE TABLE IF NOT EXISTS Article
(
Code varchar(50) NOT NULL,
Famille varchar(50) NOT NULL
)
INSERT INTO Article (Code, Famille)
VALUES ('1', 'F1'), ('2', 'F2'), ('3', 'F3');
CREATE TABLE IF NOT EXISTS Farticle
(
Designation varchar(50) NOT NULL,
Compte varchar(50) NOT NULL
)
INSERT INTO Farticle (Designation, Compte)
VALUES ('F1', '700000'), ('F2', '710000'), ('F3', '720000');
CREATE TABLE IF NOT EXISTS Ecritures
(
Compte varchar(50) NOT NULL,
Ht numeric NOT NULL
)
Мой запрос - это триггер, добавляющий несколько новых строк в Table Facture, он должен добавлять строки в Ecritures с таким же количеством строк Artfacture
declare @piece as nvarchar(50), @code as nvarchar(50)
declare @rowCount int = 0, @currentRow int = 1
select @rowCount = count(*) from ArtFacture where N = @piece;
while (@currentRow <= @rowCount)
begin
set @Code = (select code from ArtFacture where N = @piece)
set @compte = (select Compte from Farticle where Designation = (select Famille from Article where code = @code))
set @Ht = (select ht from ArtFacture where N = @piece)
insert into Ecritures (Compte,Ht)
values (@compte,@Ht)
end
Я ошибся, но не знаю где?