1. A trigger is a special type of stored procedure that automatically executes in response to certain events like insert, update or delete on a table.
2. Triggers can be used to audit changes to tables by logging information to another table when data is inserted, updated or deleted.
3. The main types of triggers in SQL Server are DML triggers for data manipulation language statements, DDL triggers for data definition language statements, and logon triggers.
1. TRIGGER
First And Most important things come to everyone mind is that “What is trigger and why
we use it????” .So trigger is nothing but just like a special kind of Store Procedure
“Trigger is special kind of store procedure or stored programs, which is automatically fired
or executed when some events (Insert, Delete, and Update) occur. “
If you write a trigger for insert operation on a table, after firing the trigger, it creates a table
named “INSERTED” in the memory. Then it performs the insert operation and after that the
statements inside the trigger executes. We can query the “INSERTED” table to manipulate
or use the inserted row/s from the trigger.
Similarly, if you write a trigger for delete operation on a table, it creates a table in memory
named “DELETED” and then deletes the row.
Why and when use trigger:
We use trigger when we want some event should happen automatically on certain
desirable scenario.
Let’s take an example.
You have a table which changes frequently, now you want to know how many times and
when did these changes take place.
In that case you can create a trigger which will insert desired data in any other table
whenever any changes in your main table occur.
Types of Triggers
In Sql Server we can create 3 types of triggers
1 Data Definition Language (DDL) triggers,
2. Data Manipulation Language (DML) triggers,
3. Logon triggers.
1. DDL Triggers
In SQL Server we can create triggers on DDL statements (like CREATE, ALTER, and DROP)
and certain system defined stored procedures that perform DDL-like operations.
2. 2. DML Triggers
In SQL Server we can create triggers on DML statements (like INSERT, UPDATE, and
DELETE) and stored procedures that perform DML-like operations. DML Triggers are of
two types
After trigger (using FOR/AFTER CLAUSE)
This type of trigger fires after SQL Server finish the execution of the action successfully that
fired it.
Example : If you insert record/row in a table then the trigger related/associated with the
insert event on this table will fire only after the row passes all the constraints, like as
primary key constraint, and some rules. If the record/row insertion fails, SQL Server will
not fire the After Trigger.
Instead of Trigger (using INSTEAD OF CLAUSE)
This type of trigger fires before SQL Server starts the execution of the action that fired it.
This is differ from the AFTER trigger, which fires after the action that caused it to fire. We
can have an INSTEAD OF insert/update/delete trigger on a table that successfully executed
but does not include the actual insert/update/delete to the table.
Example : If you insert record/row in a table then the trigger related/associated with the
insert event on this table will fire before the row passes all the constraints, such as primary
key constraint and some rules. If the record/row insertion fails, SQL Server will fire the
Instead of Trigger.
3.Logon Triggers
Logon triggers are special type of trigger that fire when LOGON event of Sql Server is
raised. This event is raised when a user session is being established with Sql Server that is
made after the authentication phase finishes, but before the user session is actually
established. Hence, all messages that we define in the trigger such as error messages, will
be redirected to the SQL Server error log. Logon triggers do not fire if authentication fails.
We can use these triggers to audit and control server sessions, such as to track login
activity or limit the number of sessions for a specific login.
In Short Different Type of triggers are:
1) DML
a) Instead of Trigger: Instead of trigger are fired in place of the triggering action such as an
3. insert, update, or delete
b) After Trigger: After trigger execute following the triggering action ,such as an
insert,update,or delete.
2) DDL Trigger
This type of trigger is fired against DDL statements like Drop Table, Create Table, Or Alter
Table,DDL Triggers are always after Triggers
3) Logon trigger
This type of trigger is fired against a LOGON event before a user session is established to
the SQL Server
Syntax: Very easy and useful syntax of trigger
CREATE TRIGGER triggerName ON table
AFTER INSERT |After Delete |After Upadte
AS BEGIN
INSERT INTO dbo.UserHistory............
END
Example:
1. Create a Table
CREATE TABLE Employee_Test
(
Emp_ID INT Identity,
Emp_name Varchar(100),
Emp_Sal Decimal (10,2)
)
2. Insert Records in It:
4. 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);
3. Create another table to store transaction records like records of insert delete
update on Employee_Test Table
CREATE TABLE Employee_Test_Audit
(
Emp_ID int,
Emp_name varchar(100),
Emp_Sal decimal (10,2),
Audit_Action varchar(100),
Audit_Timestamp datetime
)
4. Now create trigger:
CREATE TRIGGER trgAfterInsert ON [dbo].[Employee_Test]
FOR INSERT
AS
declare @empid int;
declare @empname varchar(100);
declare @empsal decimal(10,2);
5. 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.'
GO
5. Now insert one record in Employee_Test
insert into Employee_Test values('Ravi',1500);
6. After That see Employee_Test_Audit table
Select * from Employee_Test_Audit
Good luck!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!