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.
Sql-server – Insert Update trigger how to determine if insert or update
sql-servertriggerstsql
Related Question
- Sql – How to return only the Date from a SQL Server DateTime datatype
- Sql-server – How to check if a column exists in a SQL Server table
- Sql-server – Select columns from result set of stored procedure
- Sql – Insert results of a stored procedure into a temporary table
- Sql – How to do an UPDATE statement with JOIN in SQL Server
- Sql-server – Update a table using JOIN in SQL Server
- Sql – How to UPDATE from a SELECT in SQL Server
- Sql-server – Trigger for insert, update, delete
Best Solution
Triggers have special
INSERTED
andDELETED
tables to track "before" and "after" data. So you can use something likeIF EXISTS (SELECT * FROM DELETED)
to detect an update. You only have rows inDELETED
on update, but there are always rows inINSERTED
.Look for "inserted" in CREATE TRIGGER.
Edit, 23 Nov 2011
After comment, this answer is only for
INSERTED
andUPDATED
triggers.Obviously, DELETE triggers can not have "always rows in
INSERTED
" as I said above