我有2個表極限值:SQL觸發器,從插入
表1:DocNo],[NUMERIK],[MaTune]
表2:DocNo],[RowNo],[ NUMERIK],[MaTune],[DateData]
我想創建於表2觸發器:
每個值都加入到表2,值[NUMERIK]和[MaTune]應報告爲表1,其中[DocNo]是類似的
但是如果我用相同的文檔編號添加第二個值,則發現錯誤。考試結束後,我發現爲什麼,當我要求更新日期/時間時,會爲每個docnmnt報告相同的DocNo和RowNo。
我怎麼能只有值修改到我的觸發器。由於
這裏是我的代碼:
CREATE TRIGGER [dbo].[Tr_MAJ]
ON [dbo].[Table2]
AFTER INSERT,UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--Déclaration des variables
DECLARE @DocNo INT
DECLARE @RowNo SMALLINT
DECLARE @Numerik INT
DECLARE @MaTune DECIMAL(15,2)
DECLARE @DocNo_TheCat INT
--Attribution des variables
SELECT @DocNo = DocNo, @RowNo=RowNo, @Numerik = Numerik, @MaTune = Matune FROM inserted
--Mise à jour de la date et l'heure dans Table2
UPDATE Table2 SET Datedata= GETDATE() WHERE [email protected] AND [email protected]
IF (SELECT MaTune from Table1 where [email protected]) IS NULL
BEGIN
UPDATE Table1 SET MaTune = @MaTune, Numerik = @Numerik where [email protected]
END
IF (SELECT MaTune from Table1 where [email protected]) IS NOT NULL
BEGIN
--On attribue les nouvelles variables
SELECT @DocNo_TheCat = DocNo, @RowNo=RowNo, @Numerik = Numerik, @MaTune = Matune FROM TheIxTable178 where [email protected] and [email protected] and datedata = (select TOP 1 MAX(datedata) from Table2)
UPDATE Table1 SET MaTune = @MaTune, Numerik = @Numerik where [email protected]_TheCat
END
END
'inserted'可以包含多個* *行。就像這樣,像SELECT @DocNo = ... FROM插入這樣的分配被破壞了。 –
你可以限制插入的數據只有一個修改? – Chris