I need to write an Insert, Update Trigger on table A which will delete all rows from table B whose one column (say Desc) has values like the value inserted/updated in the table A’s column (say Col1). How would I go around writing it so that I can handle both Update and Insert cases. How would I determine if the trigger is executed for an update or insert.
Share
Triggers have special
INSERTEDandDELETEDtables to track "before" and "after" data. So you can use something likeIF EXISTS (SELECT * FROM DELETED)to detect an update. You only have rows inDELETEDon update, but there are always rows inINSERTED.Look for "inserted" in CREATE TRIGGER.
Edit, 23 Nov 2011
After comment, this answer is only for
INSERTEDandUPDATEDtriggers.Obviously, DELETE triggers can not have "always rows in
INSERTED" as I said above