What is it?
A trigger is almost like a command/function that executes automatically in response to another transaction that has executed. Think of it as kind of a stored procedure that executes automatically when an event occurs.
DML Triggers - After and Instead
AFTER triggers can run after an insert, update delete or merge.
INSTEAD OF trigger can override the actions of a particular DML statement. They can provide some sort of error or value checking.
When triggers are fired, there are virtual tables (called inserted and deleted) that will hold the values of the data before and after any modification.
Why use them?
Triggers are often used for auditing purposes, similar to constraints but we can create some sort of auditing or logging table. I've also read that they can be used for some sort of post data manipulation processing.
Example: Let's create a trigger in the AdventureWorks database. The purpose of this trigger is to log any new items added to the ProductModel database. I want to capture the ID, Name, and date/time the new column was inserted. Follow the directions in the comments.
#sqlserver #storedprocs #scripts #TSQL #automate #Devleopment