Hi friends,
I want to know What is Trigger ? and types of trigger with example insert, update, delete.
thank you.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted May 9, 2012, 6:39 PM EDT
Triggers are special kind of stored procedures that get executed automatically when an INSERT, UPDATE or DELETE operation takes place on a table.
What is a Trigger
A trigger is a special kind of a store procedure that executes in response to certain action on the table like insertion, deletion or updation of data. It is a database object which is bound to a table and is executed automatically. You can't explicitly invoke triggers. The only way to do this is by performing the required action no the table that they are assigned to.Types Of Triggers
There are three action query types that you use in SQL which are INSERT, UPDATE and DELETE. So, there are three types of triggers and hybrids that come from mixing and matching the events and timings that fire them.Basically, triggers are classified into two main types:-
(i)After Triggers (For Triggers)
(ii)Instead Of Triggers
(i) After Triggers
These triggers run after an insert, update or delete on a table. They are not supported for views.AFTER TRIGGERS can be classified further into three types as:
(a)AFTER INSERT Trigger.
(b)AFTER UPDATE Trigger.
(c)AFTER DELETE Trigger.
Let's create After triggers. First of all, let's create a table and insert some sample data. Then, on this table, I will be attaching several triggers.
CREATE TABLE Employee_Test ( Emp_ID INT Identity, Emp_name Varchar(100), Emp_Sal Decimal (10,2) ) INSERT INTO Employee_Test VALUES ('Anees',1000); INSERT INTO Employee_Test VALUES ('Rick',1200); INSERT INTO Employee_Test VALUES ('John',1100); INSERT INTO Employee_Test VALUES ('Stephen',1300); INSERT INTO Employee_Test VALUES ('Maria',1400);Now, create the audit table as:-
(a) AFTRE INSERT Trigger
This trigger is fired after an INSERT on the table. Let's create the trigger as:-CREATE TRIGGER trgAfterInsert ON [dbo].[Employee_Test] FOR INSERT AS declare @empid int; declare @empname varchar(100); declare @empsal decimal(10,2); declare @audit_action varchar(100); select @empid=i.Emp_ID from inserted i; select @empname=i.Emp_Name from inserted i; select @empsal=i.Emp_Sal from inserted i; set @audit_action='Inserted Record -- After Insert Trigger.'; insert into Employee_Test_Audit (Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp) values(@empid,@empname,@empsal,@audit_action,getdate()); PRINT 'AFTER INSERT trigger fired.' GOIn the trigger body, table named inserted has been used. This table is a logical table and contains the row that has been inserted. I have selected the fields from the logical inserted table from the row that has been inserted into different variables, and finally inserted those values into the Audit table.
To see the newly created trigger in action, lets insert a row into the main table as :
Now, a record has been inserted into the Employee_Test table. The AFTER INSERT trigger attached to this table has inserted the record into the Employee_Test_Audit as:-
(b) AFTER UPDATE Trigger
This trigger is fired after an update on the table. Let's create the trigger as:-CREATE TRIGGER trgAfterUpdate ON [dbo].[Employee_Test] FOR UPDATE AS declare @empid int; declare @empname varchar(100); declare @empsal decimal(10,2); declare @audit_action varchar(100); select @empid=i.Emp_ID from inserted i; select @empname=i.Emp_Name from inserted i; select @empsal=i.Emp_Sal from inserted i; if update(Emp_Name) set @audit_action='Updated Record -- After Update Trigger.'; if update(Emp_Sal) set @audit_action='Updated Record -- After Update Trigger.'; insert into Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp) values(@empid,@empname,@empsal,@audit_action,getdate()); PRINT 'AFTER UPDATE Trigger fired.' GOLet's update a record column and see what happens.
(c) AFTER DELETE Trigger
This trigger is fired after a delete on the table. Let's create the trigger as:-CREATE TRIGGER trgAfterDelete ON [dbo].[Employee_Test] AFTER DELETE AS declare @empid int; declare @empname varchar(100); declare @empsal decimal(10,2); declare @audit_action varchar(100); select @empid=d.Emp_ID from deleted d; select @empname=d.Emp_Name from deleted d; select @empsal=d.Emp_Sal from deleted d; set @audit_action='Deleted -- After Delete Trigger.'; insert into Employee_Test_Audit (Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp) values(@empid,@empname,@empsal,@audit_action,getdate()); PRINT 'AFTER DELETE TRIGGER fired.' GOLet's fire a delete on the main table.
A record has been inserted into the audit table as:-
This disables the After Delete Trigger named trgAfterDelete on the specified table.
(ii) Instead Of Triggers
These can be used as an interceptor for anything that anyonr tried to do on our table or view. If you define an Instead Of trigger on a table for the Delete operation, they try to delete rows, and they will not actually get deleted (unless you issue another delete instruction from within the trigger)INSTEAD OF TRIGGERS can be classified further into three types as:-
(a)INSTEAD OF INSERT Trigger.
(b)INSTEAD OF UPDATE Trigger.
(c)INSTEAD OF DELETE Trigger.
(a) Let's create an Instead Of Delete Trigger as:-
CREATE TRIGGER trgInsteadOfDelete ON [dbo].[Employee_Test] INSTEAD OF DELETE AS declare @emp_id int; declare @emp_name varchar(100); declare @emp_sal int; select @emp_id=d.Emp_ID from deleted d; select @emp_name=d.Emp_Name from deleted d; select @emp_sal=d.Emp_Sal from deleted d; BEGIN if(@emp_sal>1200) begin RAISERROR('Cannot delete where salary > 1200',16,1); ROLLBACK; end else begin delete from Employee_Test where Emp_ID=@emp_id; COMMIT; insert into Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp) values(@emp_id,@emp_name,@emp_sal,'Deleted -- Instead Of Delete Trigger.',getdate()); PRINT 'Record Deleted -- Instead Of Delete Trigger.' end END GONow, let's try to delete a record with the Emp_Sal >1200 as:-
And this record will not be deleted.
In a similar way, you can code Instead of Insert and Instead Of Update triggers on your tables.
Please refer the below links
http://www.c-sharpcorner.com/uploadfile/shashikantray/create-delete-and-update-triggers-in-a-database/default.aspx
http://www.codeproject.com/Articles/25600/Triggers-Sql-Server
Thanks
Jignesh TrivediRank #16 · 50.3K reputationPosted May 9, 2012, 7:53 PM EDT
A trigger is a database object that is attached to a table. In many aspects it is similar to a stored procedure.
In other word special kind of stored procedure that run automatically on table when DML is fire.
Trigger is Data base object so it limited by number of objects in a database.
please refer
http://www.sqlteam.com/article/an-introduction-to-triggers-part-i
hope this help.
MuthuMari MRank #5,738 · 288 reputationPosted May 9, 2012, 9:40 PM EDT
A trigger is a special kind of a store procedure that executes in response to certain action on the table like insertion, deletion or updation of data. It is a database object which is bound to a table and is executed automatically. You can't explicitly invoke triggers. The only way to do this is by performing the required action no the table that they are assigned to.
Types Of Triggers
There are three action query types that you use in SQL which are INSERT, UPDATE and DELETE. So, there are three types of triggers and hybrids that come from mixing and matching the events and timings that fire them.Basically, triggers are classified into two main types:-
(i)After Triggers (For Triggers)
(ii)Instead Of Triggers
pls refer this url
http://www.codeproject.com/Articles/25600/Triggers-Sql-Server
http://docs.oracle.com/cd/B19306_01/server.102/b14220/triggers.htm#i2013
thanks.
If this post is useful then mark it as "Accepted Answer"