Additional Information. Understanding SQL Server inserted and deleted tables for DML triggers · Foreign Key vs. Trigger Referential Integrity in SQL Server. This article gives a brief introduction about Triggers in SQL Server /; Author: Sudipta Chaudhari; Updated: 26 Apr ; Section. What exactly is a trigger in SQL Server? We cover this and show you how to find this “hidden” code in your database.

Author: Kazrarisar Tasar
Country: Anguilla
Language: English (Spanish)
Genre: Finance
Published (Last): 5 June 2014
Pages: 205
PDF File Size: 8.62 Mb
ePub File Size: 15.88 Mb
ISBN: 638-4-35060-435-5
Downloads: 10231
Price: Free* [*Free Regsitration Required]
Uploader: Moogukazahn

This article explains the After Trigger and Instead of Trigger using an example and the differences between them. This article explains the “After Trigger” and “Instead of Trigger” using an example and the differences between them but first you need to look at an overview of both.

After Trigger vs Instead of Trigger in SQL Server

These kinds of triggers fire before the execution of an action query that can only be DML statements like Insert, Update and Delete but after the execution of that query. The table data will not be affected, in other words if you want to insert or update the data of the table then you need to write it in the trigger using “inserted” or “deleted” virtual tables.


Create a dispxradores of a table named “Employee” in the database for performiing an action such as insert. Create a schema table named “Logs” that will contain the activity of the trigger.

I eql an After Trigger that executes an insertion in the “Logs” table when we insert the data in the “Employee” table. Check below for the details. Create a Trigger where, at the time of insertion in the “Employee” table, we insert a record in the “Logs” table. Insert the data in the “Employee” table that executes a trigger automatically and selects both tables to check the data.

Insert into Employee values 1,’Rahul’,’Finance’. If I alter the preceding After Trigger to be an Instead of Trigger that executes disparqdores insertion into the “Logs” table when we fire the insertion query into the “Employee” table then the data will not be inserted into the Employee table.

Working With Triggers

Alter the preceding trigger such that at the time of insertion into the “Employee” table we insert a record into the “Logs” table. Insert the data in the “Employee” table that executes the trigger automatically. Insert into Employee values 2,’Bansal’,’IT’.


Insertion and Update in the Table: As you can see, the data has been inserted into the “Employee” table without using an “inserted’ virtual table. When I used an “After Trigger” and if I use “inserted” then it will give a primary key violation error. Alter the trigger and insert the data via the “inserted” virtual table.

Inserting the new data will generate an error because the action query will fire and wants to insert the data whereas the “inserted” virtual table also wants to insert the same row. If I create an instead of trigger using the “inserted” virtual table then the data will be inserted.

Request a new Category View All.

Rahul Bansal Dec 18 Visual Studio New Features.

Author: admin