Introduction
What is a Trigger?
When we use the triggers or advantages of Triggers
Triggers can be written for the following purposes,
- To generate some derived column values automatically
- To enforce referential integrity
- To do event logging and store information on table access
- To audit
- To perform synchronous replication of tables
- To impose security authorizations
- To prevent the invalid transactions
In SQL Server, There are three types of triggers,
- DDL Trigger
- DML Trigger
- Logon Trigger
DDL Trigger
The DDL Trigger will execute whenever you will do insert, update, delete and drop operations.
DML Trigger
DML Trigger is fired automatically in the response of DML statements INSERT, UPDATE, DELETE statements. In this blog, we will discuss DML Triggers with examples. Let’s say, the price of product changes constantly, it is important to maintain the history of the prices of products. Here, we are going to create two tables first Product and second Product_history.
- CREATE TABLE Product
- (
- product_id int primary key identity,
- product_name varchar(40),
- unit_price money
- );
- INSERT INTO Product values('L538',7000)
- INSERT INTO Product values('L550',7000)
- INSERT INTO Product values('L551',7000)
- SELECT * FROM product
Output
- CREATE TABLE Product_histroy
- (
- Id int primary key identity,
- Product_histroy varchar(200),
- );
- SELECT * FROM Product_histroy
Syntax
- CREATE TRIGGER Trigger_Name
- ON Table_Name
- {INSERT [OR] | UPDATE [OR] | DELETE}
- AS
- Begin
- --Declaration Part
- --Select Part
- --Executable-Code
- --EXCEPTION
- --Exception-Handling-Code
- END;
DML Trigger can be classified into two types,
- After Trigger
- Instead Of trigger
After triggers fire after triggering action
The insert, update, delete statement causes the after trigger to fire after their respective statement to complete execution. After triggers are invoked after DML (insert, update and delete) operations. They are not supported for views. And after, the trigger is also divided into the following 3 parts,
- After Insert
- After Delete
- After Update
After INSERT
Let’s have look at after insert trigger below.
Syntax
- CREATE TRIGGER UTRG_TriggerName
- ON
- TableName
- FOR INSERT
- AS BEGIN
- --Declaration Part
- --Select Part
- --Executable-Code
- --EXCEPTION
- --Exception-Handling-Code
- END
We are going to write the trigger on Product and whenever a new model gets launched or the old model's price is updated or the old vehicle model is deleted from the table then it has some record in another table ‘Product_History’. We have used ‘Product_History’ table for the same purpose.
Example
- CREATE TRIGGER UTRG_Product_Insert
- ON
- Product
- FOR INSERT
- AS BEGIN
- DECLARE @product_id int, @product_name varchar(40), @unit_price money
- SELECT @product_id = product_id, @product_name = product_name, @unit_price = unit_price FROM inserted
- INSERT INTO Product_histroy VALUES('New product with Id ' + cast (@product_id As varchar(40)) +' product_name '+ @product_name + 'on date ' + cast (getdate() As varchar(40)))
- END
Output

In the above trigger, we have selected the column value from the ‘inserted’ table, this table is nothing but a temporary table, whenever you do any insert operation intermittent in trigger only we can select the values and do the required operation as per your requirement. Execute the below code to check if the records are affected or not in the below tables.
- select * from Product
- select * from Product_histroy
Output
If you wanted to do some changes in the above trigger use ALTER command and do the respective changes as per the project requirement.
Alter Trigger
- ALTER TRIGGER UTRG_Product_Insert
- ON
- Product
- FOR INSERT
- AS BEGIN
- DECLARE @product_id int, @product_name varchar(40), @unit_price money
- SELECT @product_id = product_id, @product_name = product_name, @unit_price = unit_price FROM inserted
- INSERT INTO Product_histroy VALUES('New product with Id ' + cast (@product_id As varchar(40)) +' product_name '+ @product_name + ' on date ' + cast (getdate() As varchar(40))+ ' cost is ' + cast( @unit_price as varchar(40)))
- END
- INSERT INTO Product VALUES('L575',14000)
- select * from Product
- select * from Product_histroy
Output

After Update
An after update trigger is called immediately whenever any type of update operation is done on a table. Now we are going to create update trigger for update operation like below.
If you want to print any message in trigger then you can use PRINT command.
Syntax
- CREATE TRIGGER UTRG_Product_Update
- ON
- Product
- FOR UPDATE
- AS BEGIN
- --Declaration Part
- --Select Part
- --Executable-Code
- --EXCEPTION
- --Exception-Handling-Code
- END
Example
- CREATE TRIGGER UTRG_Product_Update
- ON
- Product
- FOR UPDATE
- AS BEGIN
- DECLARE @product_id int
- DECLARE @product_name varchar(40)
- DECLARE @unit_price varchar(40)
- SELECT @product_id = product_id FROM deleted
- SELECT @product_name = product_name FROM deleted
- SELECT @unit_price = unit_price FROM deleted
- INSERT INTO Product_histroy VALUES('Product with Id ' + cast (@product_id As varchar(40)) +' cost '+ cast (@unit_price As varchar(40)) +' is updated on date '+cast(getdate() as varchar(40)))
- END









Pravas RanjanPosted Sep 12, 2018, 5:16 AM
Excellent article Jitendra Bhai, please make an article of Cursor with detail explanation with code example
Sundaram SubramanianPosted Sep 11, 2018, 2:44 AM
Great one. Thanks for the article
Saineshwar BageriPosted Sep 10, 2018, 8:56 AM
Try to write on new features, not on common features there are lot's of article on this topic
Rajesh KumarPosted Sep 10, 2018, 12:03 AM
Very use full article, Thanks for sharing.............
Vineet DubeyPosted Sep 9, 2018, 11:09 PM
Sir, This Article is very helpful thanku so much,
Jignesh KumarPosted Sep 8, 2018, 5:55 AM
Very good article thank you for sahring
Mahesh ChandPosted Sep 8, 2018, 5:39 AM
Nicely done!! Thanks Jitendra.