jeudi 17 décembre 2015

Trigger SQL Server with IF

I have the follow IF:

IF (NOT UPDATE([Field1]) AND (((SELECT Field2 FROM inserted) = 'Finished') OR ((SELECT Field2 FROM inserted) = 'Closed')))
BEGIN
 ... some code that updates a Field1
END

(The code in the IF is working nice so I omitted it)

My IF now makes to UPDATE the Field1 when I update to 'Finished' or 'Closed' if the [Field1] is not inserted. But if I Update after the row, the trigger will jump and update again the [Field1] and I want to avoid this (Field1 is a historical field that saves the data when the row get 'Finished' or 'Closed'). I tried adding this:

AND ((SELECT Table1.[Field1] FROM Table1, inserted WHERE Table1.[Id] = inserted.[Id]) = '') 

But then the trigger never jumps.

What I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire