0% found this document useful (0 votes)
122 views

Introduction To Triggers in SQL Server

SQL Server triggers allow executing code before or after data modifications (insert, update, delete) on a table or view. There are two types of triggers: AFTER triggers run after the modification has completed, while INSTEAD OF triggers run before and can override the modification. Triggers can be used to log changes to an audit table or enforce business rules.

Uploaded by

arun vilwanathan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Introduction To Triggers in SQL Server

SQL Server triggers allow executing code before or after data modifications (insert, update, delete) on a table or view. There are two types of triggers: AFTER triggers run after the modification has completed, while INSTEAD OF triggers run before and can override the modification. Triggers can be used to log changes to an audit table or enforce business rules.

Uploaded by

arun vilwanathan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction to Triggers in SQL Server

SQL Server Triggers are used to execute after or before an INSERT, DELETE, or an UPDATE
operation on a table. You can use these SQL triggers on Views, or Tables to perform any of
the above-specified activities. Remember, you can associate a trigger to a single table only.

Types of Triggers in SQL Server

There are two types of Triggers in SQL Server, and they are AFTER TRIGGERS, and
INSTEAD OF TRIGGERS.

For the Sql Server triggers demonstration, we are working with the Employee table and
Employee audit table

AFTER TRIGGERS in SQL Server

The after/for triggers in SQL runs after an INSERT, DELETE, or an UPDATE on a table. It
means, Before the trigger starts running, all the operations should execute, and the
statement has to succeed in the constraint check as well.

After triggers in SQL Server are not supported on Views so, use them on tables only. It can
further divide into

AFTER INSERT TRIGGERS: This trigger will fire after the completion of Insert operation on
the Employee table. Once it completes inserting into the Employee table, it will start
inserting into the audit table. Say, if it fails to insert into the Employee table, then it won’t
insert into the Audit table.

AFTER UPDATE TRIGGERS: This SQL Server trigger will fire after the Update operation
completed on the Employees table. Once it ends Updating the Employee table, it will start
inserting/updating into the audit table. For instance, if it fails to update the Employees table,
then it won’t insert into the Audit table.

AFTER DELETE TRIGGERS: The After delete trigger will fire after the completion of Delete
operation on the Employee table. Once it completes deleting the records from the
Employees, it will start inserting/deleting from the audit table. Say, if it fails to remove from
the Employee table, then it won’t insert into the Audit table

INSTEAD OF TRIGGERS in SQL Server

SQL Server Instead of Triggers fired before the execution of an INSERT, DELETE, or an
UPDATE on a table start.

It means, Before the trigger starts running, it does not need any condition constraint check.
So, this trigger will execute even the constraint check fails. It can further divide into

INSTEAD OF INSERT TRIGGERS: This instead of insert trigger will fire before the Insert
operation on Employee table starts. Once it completes inserting into the Employee Audit
table, it will begin to insert into the Employee table. And if it fails for some reason, it won’t
insert into the Employee table.
INSTEAD OF UPDATE TRIGGERS: This update trigger will fire before updating the records
in the Employee table. Once it completes the trigger execution, it will start updating the
records in the Employee table. And if it fails, it won’t update the table.

INSTEAD OF DELETE TRIGGERS: This delete trigger will fire before deleting records from
the Employee table starts. Once the trigger executed successfully, it will begin deleting
records from the Employees table. And if it fails, it won’t remove any record from the
Employee table.

Sql Server Triggers Syntax

The syntax of After Triggers in SQL Server is

-- Create Triggers in SQL Server

CREATE [OR ALTER] TRIGGER [Schema_Name].Trigger_Name

ON Table

AFTER INSERT | UPDATE | DELETE

AS

BEGIN

-- Trigger Statements

-- Insert, Update, Or Delete Statements

END

Schema_name: Please specify the schema name. For example, dbo, or Human Resource, etc.

Trigger_Name: You can specify any name you wish to give other than the system reserved
keywords. Please try to use meaningful names so that you can identify them easily.

The basic syntax of Instead Of Triggers in SQL Server is as shown below:

-- Create Triggers in SQL Server

CREATE [OR ALTER] TRIGGER [Schema_Name].Trigger_Name

ON Table | View

INSTEAD OF INSERT | UPDATE | DELETE

AS

BEGIN

-- Trigger Statements

-- Insert, Update, Or Delete Statements

END
Let us see how to Create a Trigger, Modify the existing triggers, and delete triggers in SQL
Server with an example.

For this SQL Server triggers demonstration, We are going to use the below-shown tables. As
you can see, the table is Empty

You might also like