SQL Server: Triggers
In this article i will explain what is Trigger in SQL Server.
In previous articles i explained about joins, Batches, Stored procedure, Functions, and Difference between Stored procedure and Function in SQL Server. Now i will explain what is Trigger in SQL Server.
A Trigger is a database object which automatically executes whenever some operation like insert, update or delete is performed on table.
Create Trigger <Trigger-Name> on <Table-Name> For insert, update, delete As Begin -- Your operations here. print 'Done some change.' End
Create Trigger EmpTrigger on Employee For insert, update, delete As Begin -- Your operations here. print 'Done some change.' End
Check the output of Employee table.
select * from Employee
Execute above statement, then Trigger will be created on Employee table.
Now i'm inserting one new record into Employee table.
insert into Employee(EmployeeName, Email, DepartmentID, Active) values('Vinod Reddy', 'firstname.lastname@example.org', 1, 1)
After execution above statement, EmpTrigger will be fired and the operations which are mentioned in that Trigger will be performed. It will give following message.
Done some change.
(1 row(s) affected)
CreatedDec 22, 2013
UpdatedDec 22, 2013