Beruflich Dokumente
Kultur Dokumente
riggers are special type of stored procedure that automatically execute when a DDL or DML statement
associated with the trigger is executed. DML Triggers are used to evaluate data after data manipulation
Example
1. -- First create table Employee_Demo
2. CREATE TABLE Employee_Demo
3. (
http://www.dotnet-tricks.com/Tutorial/sqlserver/OPUH170312-After-Trigger,-Instead-of-Trigger-Example.html
1/11
7/22/2014
http://www.dotnet-tricks.com/Tutorial/sqlserver/OPUH170312-After-Trigger,-Instead-of-Trigger-Example.html
2/11
7/22/2014
1.
1.
--now select data from both the tables to see trigger action
Trigger have inserted the new record to Employee_Demo_Audit table for insert statement. In this way we
can trace a insert activity on a table using trigger.
3/11
7/22/2014
12. insert
intoEmployee_Demo_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
13. values (@empid,@empname,@empsal,@audit_action,getdate());
14. PRINT 'AFTER UPDATE trigger fired.'
15. --Output will be
1.
1.
--now select data from both the tables to see trigger action
Trigger have inserted the new record to Employee_Demo_Audit table for update statement. In this way we
http://www.dotnet-tricks.com/Tutorial/sqlserver/OPUH170312-After-Trigger,-Instead-of-Trigger-Example.html
4/11
7/22/2014
1.
1.
--now select data from both the tables to see trigger action
http://www.dotnet-tricks.com/Tutorial/sqlserver/OPUH170312-After-Trigger,-Instead-of-Trigger-Example.html
5/11
7/22/2014
Trigger have inserted the new record to Employee_Demo_Audit table for delete statement. In this way we
can trace a delete activity on a table using trigger.
Now I am going to explain the use of Instead of Trigger using Insert, Update, Delete statement with example
BEGIN TRAN
12.
SET NOCOUNT ON
13.
if(@emp_sal>=1000)
14.
begin
15.
16.
http://www.dotnet-tricks.com/Tutorial/sqlserver/OPUH170312-After-Trigger,-Instead-of-Trigger-Example.html
6/11
7/22/2014
values(@@identity,@emp_name,@emp_sal,@audit_action,getdate());
17.
COMMIT;
18.
19. END
20. --Output will be
1.
1.
--now select data from both the tables to see trigger action
Trigger have inserted the new record to Employee_Demo_Audit table for insert statement. In this way we
can apply business validation on the data to be inserted using Instead of trigger and can also trace a insert
activity on a table.
http://www.dotnet-tricks.com/Tutorial/sqlserver/OPUH170312-After-Trigger,-Instead-of-Trigger-Example.html
7/11
7/22/2014
BEGIN TRAN
11. if(@emp_sal>=1000)
12.
begin
13.
14.
else begin
15.
insert into
Employee_Demo_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@emp_id,@emp_name,@emp_sal,@audit_action,getdate());
16.
COMMIT;
17.
1.
1.
--now select data from both the tables to see trigger action
http://www.dotnet-tricks.com/Tutorial/sqlserver/OPUH170312-After-Trigger,-Instead-of-Trigger-Example.html
8/11
7/22/2014
Trigger have inserted the updated record to Employee_Demo_Audit table for update statement. In this way
we can apply business validation on the data to be updated using Instead of trigger and can also trace a
update activity on a table.
10.
ROLLBACK;
11.
end
12.
else begin
13.
14.
COMMIT;
15.
insert into
Employee_Demo_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
16.
17.
9/11
7/22/2014
1.
1.
--now select data from both the tables to see trigger action
Trigger have inserted the deleted record to Employee_Demo_Audit table for delete statement. In this way
we can apply business validation on the data to be deleted using Instead of trigger and can also trace a
delete activity on a table.
10/11
7/22/2014
article your sql triggers concepts will be strong. I would like to have feedback from my blog readers. Please post
your feedback, question, or comments about this article.
Print Article
Shailendra Chauhan works as Sr. Software Engineer at reputed MNC and has more than 5 years of hand
over Microsoft .NET technologies. He is a .NET Consultant and is the founder & chief editor of www.dotnettricks.com and www.dotnetinterviewtricks.com blogs. He is an author of books ASP.NET MVC Interview
Questions and Answers & LINQ Interview Questions and Answers.
He loves to work with web applications and mobile apps using Microsoft technology including ASP.NET,
MVC, C#, SQL Server, WCF, Web API, Entity Framework,Cloud Computing, Windows Azure, jQuery, jQuery
Mobile, Knockout.js, Angular.js and many more web technologies. More...
http://www.dotnet-tricks.com/Tutorial/sqlserver/OPUH170312-After-Trigger,-Instead-of-Trigger-Example.html
11/11