我有一个合并语句应该多次执行一次触发器。
我首先认为我的触发器没有执行,但通过一些研究我发现每个语句只触发一次触发器(触发器是一个语句)。
但那里的所有帖子都很旧,我认为现在可能有一种简单的方法可以让我的触发器多次执行。
那么有什么东西可以添加到我的触发器或我的合并语句中以使我的触发器这样做吗?
由于
TRIGGER
TRIGGER [dbo].[Sofi_TERA_Trigger]
ON [dbo].[ZZ]
AFTER INSERT,UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF EXISTS(SELECT 1 FROM inserted WHERE inserted.Statut LIKE '%CLOT%' OR inserted.Statut LIKE '%CLTT%' OR inserted.Statut LIKE '%CONF%')
BEGIN
DECLARE @Id int;
DECLARE @Matricule varchar(10);
DECLARE @IdAction int;
DECLARE @NumeroOF int;
SELECT @NumeroOF = inserted.Ordre from inserted;
DECLARE OF_CURSOR CURSOR
LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
SELECT Id,Log.Matricule,IdAction from Log inner join (select max(Id) as maxID,Matricule from LOG where Log.NumeroOF = @NumeroOF group by Matricule) maxID
on maxID.maxID = Log.Id where Log.NumeroOF = @NumeroOF;
OPEN OF_CURSOR
FETCH NEXT FROM OF_CURSOR INTO @Id,@Matricule,@IdAction
WHILE @@FETCH_STATUS = 0
BEGIN
IF @IdAction!=13
BEGIN
IF @IdAction<=2
BEGIN
insert into Log(NumeroOF,Matricule,IdAction,Date,EstAdmin) values (@NumeroOF,@Matricule,13,GETDATE(),1);
END
ELSE
BEGIN
insert into Log(NumeroOF,Matricule,IdAction,Date,EstAdmin) values (@NumeroOF,@Matricule,2,GETDATE(),1);
insert into Log(NumeroOF,Matricule,IdAction,Date,EstAdmin) values (@NumeroOF,@Matricule,13,GETDATE(),1);
END
END
FETCH NEXT FROM OF_CURSOR INTO @Id,@Matricule,@IdAction
END
CLOSE OF_CURSOR;
DEALLOCATE OF_CURSOR;
END
END
合并声明
Merge ZZ AS TARGET USING ZZTemp AS SOURCE
ON (Target.Operation=Source.Operation AND Target.Ordre=Source.Ordre)
WHEN MATCHED THEN
UPDATE SET TARGET.DateTERA=SOURCE.DateTERA, TARGET.MatTERA=SOURCE.MatTERA, TARGET.MatTERC=SOURCE.MatTERC
WHEN NOT MATCHED THEN
INSERT(Operation,Ordre,ElementOTP,Article,DesignationOF,PosteTravail,ValeurTemps,DHT,Statut,StatutOF,TexteActivite,DateTERA,MatTERA,MatTERC,StatutMat)
VALUES(SOURCE.Operation,SOURCE.Ordre,SOURCE.ElementOTP,SOURCE.Article,SOURCE.DesignationOF,SOURCE.PosteTravail,SOURCE.ValeurTemps,SOURCE.DHT,
SOURCE.Statut,SOURCE.StatutOF,SOURCE.TexteActivite,SOURCE.DateTERA,SOURCE.MatTERA,SOURCE.MatTERC,SOURCE.StatutMat);
答案 0 :(得分:2)
您的问题是光标被错误地写入以处理数据集。任何将值表单设置为插入或删除到标量变量的触发器都是不正确的,并且出于数据完整性的原因必须重写。这个触发器是错误的。期。没有必须重写(以及使用相同技术的任何其他人)。
触发器中的代码应该是:
INSERT INTO Log(NumeroOF,Matricule,IdAction,Date,EstAdmin)
SELECT max(Id),l.Matricule,l.IdAction, 13,GETDATE(),1
FROM Log l
JOIN Inserted i ON l.NumeroOF = i.Ordre
WHERE i.Statut LIKE '%CLOT%' OR i.Statut LIKE '%CLTT%' OR i.Statut LIKE '%CONF%'
GROUP BY l.Matricule,l.IdAction
INSERT INTO Log(NumeroOF,Matricule,IdAction,Date,EstAdmin)
SELECT max(Id),l.Matricule,l.IdAction, 2,GETDATE(),1
FROM Log l
JOIN Inserted i ON l.NumeroOF = i.Ordre
WHERE IdAction<=2
WHERE i.Statut LIKE '%CLOT%' OR i.Statut LIKE '%CLTT%' OR i.Statut LIKE '%CONF%'
GROUP BY l.Matricule,l.IdAction
确保使用单个记录和多个记录插入进行测试,因为应测试所有触发器。一旦您确信触发器正确,请尝试使用MERGE。