Triggers In SQL Server

A SQL trigger is a database object just like a stored procedure or we can say it is a special kind of Stored Procedure that automatically fires when an event occurs in a database. Learn what is a trigger in SQL Server and how to create triggers on a database table.

A SQL trigger is a database object just like a stored procedure, or we can say it is a special kind of stored procedure which fires when an event occurs in a database. We can execute a SQL query that will "do something" in a database when an event is fired.
 
For example, a trigger can be set on a record insert in a database table. For example, if you want to increase the number of count of blogs in the Reports table when a new record is inserted in the Blogs table, we can create a trigger on the Blogs's table on INSERT and update the Reports table by increaing blog count to 1. 
 

Difference Between a Stored Procedure and a Trigger

 
Triggers are fired implicitly while stored procedures are fired explicitly.
 

Types of Triggers

 
There are two types of triggers:
  1. DDL Trigger
  2. DML Trigger

DDL Triggers

 
The DDL triggers are fired in response to DDL (Data Definition Language) command events that start with Create, Alter and Drop, such as Create_table, Create_view, drop_table, Drop_view and Alter_table.

Code of a DDL Trigger

  1. create trigger saftey  
  2. on database  
  3. for  
  4. create_table,alter_table,drop_table  
  5. as  
  6. print'you can not create ,drop and alter table in this database'  
  7. rollback

When we create, alter or drop any table in a database then the following message appears:

DDl-trigers-in-sql.jpg

DML Triggers

 
The DML triggeres are fired in response to DML (Data Manipulation Language) command events that start with with Insert, Update and Delete. Like insert_table, Update_view and Delete_table.

  1. create trigger deep  
  2. on emp  
  3. for  
  4. insert,update,delete  
  5. as  
  6. print'you can not insert,update and delete this table i'  
  7. rollback;

When we insert, update or delete in a table in a database then the following message appears,

dml-triggers-in-sql.jpg

There are two types of DML triggers


AFTER Triggers

AFTER triggers are executed after the action of an INSERT, UPDATE, or DELETE statement.

  1. create trigger insertt  
  2. on emp  
  3. after insert  
  4. as  
  5. begin  
  6. insert into empstatus values('active')  
  7. end   
aftertrigger-in-sql.jpg

INSTEAD Of Triggers

It will tell the database engine to execute the trigger instead of executing the statement. For example an insert trigger executes when an event occurs instead of the statement that would insert the values in the table . 
  1. CREATE TRIGGER instoftr  
  2. ON v11  
  3. INSTEAD OF INSERT  
  4. AS  
  5. BEGIN  
  6. INSERT INTO emp  
  7. SELECT I.id, I.names  
  8. FROM INSERTED I  
  9.    
  10. INSERT INTO emp1values  
  11. SELECT I.id1, I.name1  
  12. FROM INSERTED I  
  13. END   

When we insert data into a view by the following query then it inserts values in both tables :

  1. insert into v11 values(1,'d','dd')  

You can see both tables by the folowing query:

  1. select * from emp  
  2. select * from emp1values  
insteedofcorsor-in-sql.jpg

Summary

 
In this article I described triggers in SQL Server. I hope this article has helped you in understanding this topic. Please share it. If you know more about this, your feedback and constructive contributions are welcome.