私は2つのテーブルを持っています:
表1 : [DocNo],[Numerik],[[MaTune]
表 2 : [DocNo]、[RowNo]、[Numerik]、[MaTune]、[DateData]
Table2 にトリガーを作成したいと思います。
各値が table2 に追加され、[Numerik] と [MaTune] の値が [DocNo] が類似している table1 に報告されます。
しかし、同じドキュメント番号で 2 番目の値を追加すると、エラーが見つかりました。調べてみると、更新日時を尋ねたところ、ドキュメントごとに同じ 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 RowNo=@RowNo AND DocNo=@DocNo
IF (SELECT MaTune from Table1 where DocNo=@DocNo) IS NULL
BEGIN
UPDATE Table1 SET MaTune = @MaTune, Numerik = @Numerik where DocNo=@DocNo
END
IF (SELECT MaTune from Table1 where DocNo=@DocNo) IS NOT NULL
BEGIN
--On attribue les nouvelles variables
SELECT @DocNo_TheCat = DocNo, @RowNo=RowNo, @Numerik = Numerik, @MaTune = Matune FROM TheIxTable178 where DocNo=@DocNo and RowNo=@RowNo and datedata = (select TOP 1 MAX(datedata) from Table2)
UPDATE Table1 SET MaTune = @MaTune, Numerik = @Numerik where DocNo=@DocNo_TheCat
END
END