Using test UPDATE(columnname) , you can check the launch, regardless of whether the column has been updated (and then take specific actions), but you cannot start the trigger only when updating a specific column. It will fire as soon as the update is completed, regardless of which column was the object of the update.
So, if you think that you need to use the INSTEAD OF UPDATE trigger, you need to implement two types of actions in it:
1) insert into tbDeletedUsers + remove from tbUsers - when IsDeleted updated (or, more precisely, updated and set to 1 );
2) update tbUsers usually - when IsDeleted not updated (or updated, but not set to 1 ).
Since more than one row can be updated with one UPDATE , you may also need to consider that some rows may have IsDeleted set to 1 and others not.
I'm not a big fan of INSTEAD OF triggers, but if I really needed to use one for a task like yours, I could skip the UPDATE() test and implement the trigger as follows:
CREATE TRIGGER trg_ArchiveUsers ON tbUsers INSTEAD OF UPDATE AS BEGIN UPDATE tbUsers SET column = INSERTED. column , … FROM INSERTED WHERE INSERTED. key = tbUsers. key AND INSERTED.IsDeleted = 0 ; DELETE FROM tbUsers FROM INSERTED WHERE INSERTED. key = tbUsers. key AND INSERTED.IsDeleted = 1 ; INSERT INTO tbDeletedUsers ( columns ) SELECT columns FROM INSERTED WHERE IsDeleted = 1 ; END
Andriy m
source share