INSTEAD OF Trigger
Learn how INSTEAD OF Triggers work in SQL Server, when they are used and how they can control INSERT, UPDATE and DELETE operations.
What is an INSTEAD OF Trigger?
An INSTEAD OF Trigger is a special SQL Server trigger that executes instead of the original INSERT, UPDATE or DELETE operation.
In other words, when a user performs an operation on a table or view, SQL Server executes the INSTEAD OF Trigger rather than directly performing that operation.
- It can be created for INSERT operations.
- It can be created for UPDATE operations.
- It can be created for DELETE operations.
- It is commonly useful with views.
- It allows us to control what happens when INSERT, UPDATE or DELETE is attempted.
AFTER Trigger vs INSTEAD OF Trigger
| AFTER Trigger | INSTEAD OF Trigger |
|---|---|
| Executes after the operation. | Executes instead of the operation. |
| The original operation happens first. | The original operation is replaced by the trigger logic. |
| Commonly used for auditing and logging. | Commonly used to control or customize INSERT, UPDATE and DELETE operations. |
| Commonly used with tables. | Very useful with views. |
Basic Syntax
CREATE TRIGGER TriggerName
ON TableName
INSTEAD OF INSERT
AS
BEGIN
-- Trigger logic
END;
Example Table
Let's create a simple Students table.
CREATE TABLE Students
(
StudentId INT PRIMARY KEY,
StudentName VARCHAR(100),
City VARCHAR(50),
Age INT
);
Insert Sample Data
INSERT INTO Students
(
StudentId,
StudentName,
City,
Age
)
VALUES
(
101,
'Rahul',
'Pune',
21
);
INSTEAD OF INSERT Trigger
An INSTEAD OF INSERT Trigger executes instead of the normal INSERT operation.
CREATE TRIGGER trg_InsteadOfInsertStudent
ON Students
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO Students
(
StudentId,
StudentName,
City,
Age
)
SELECT
StudentId,
StudentName,
City,
Age
FROM inserted;
END;
The inserted Logical Table
SQL Server provides a special logical table named inserted inside an INSERT trigger.
It contains the records that were supplied by the INSERT operation.
INSERT INTO Students
(
StudentId,
StudentName,
City,
Age
)
VALUES
(
102,
'Priya',
'Mumbai',
22
);
The new row can be accessed inside the trigger through the inserted logical table.
INSTEAD OF UPDATE Trigger
An INSTEAD OF UPDATE Trigger executes instead of the normal UPDATE operation.
CREATE TRIGGER trg_InsteadOfUpdateStudent
ON Students
INSTEAD OF UPDATE
AS
BEGIN
UPDATE S
SET
S.StudentName = I.StudentName,
S.City = I.City,
S.Age = I.Age
FROM Students S
INNER JOIN inserted I
ON S.StudentId = I.StudentId;
END;
UPDATE Example
UPDATE Students
SET City = 'Pune'
WHERE StudentId = 102;
Instead of directly processing the UPDATE, SQL Server executes the INSTEAD OF UPDATE Trigger.
INSTEAD OF DELETE Trigger
An INSTEAD OF DELETE Trigger executes instead of the normal DELETE operation.
CREATE TRIGGER trg_InsteadOfDeleteStudent
ON Students
INSTEAD OF DELETE
AS
BEGIN
DELETE S
FROM Students S
INNER JOIN deleted D
ON S.StudentId = D.StudentId;
END;
DELETE Example
DELETE FROM Students
WHERE StudentId = 102;
The INSTEAD OF DELETE Trigger executes and controls how the deletion should happen.
INSTEAD OF Trigger with a View
One of the important uses of INSTEAD OF Triggers is working with views.
A view may contain data from multiple tables. In such situations, SQL Server may not allow a direct INSERT, UPDATE or DELETE operation on the view.
An INSTEAD OF Trigger can be used to control the operation and perform the required changes on the underlying tables.
Example View
CREATE VIEW StudentView
AS
SELECT
StudentId,
StudentName,
City,
Age
FROM Students;
INSTEAD OF DELETE on a View
CREATE TRIGGER trg_DeleteStudentView
ON StudentView
INSTEAD OF DELETE
AS
BEGIN
DELETE S
FROM Students S
INNER JOIN deleted D
ON S.StudentId = D.StudentId;
END;
Now a DELETE operation performed on the view can be controlled by the trigger.
inserted and deleted Logical Tables
| Logical Table | Used For |
|---|---|
| inserted | New rows involved in INSERT or UPDATE. |
| deleted | Old rows involved in DELETE or UPDATE. |
Important Points
- INSTEAD OF means the trigger executes instead of the original operation.
- It can be used with INSERT, UPDATE and DELETE.
- It is particularly useful with views.
- The inserted and deleted logical tables can be used inside the trigger.
- The trigger can contain custom logic before performing the actual database operation.
- The original operation does not automatically happen; the trigger must perform the required operation when appropriate.
Practical Uses
- Controlling INSERT operations.
- Controlling UPDATE operations.
- Controlling DELETE operations.
- Handling modifications on complex views.
- Applying custom business rules.
- Redirecting operations to underlying tables.
Summary
An INSTEAD OF Trigger executes instead of the original INSERT, UPDATE or DELETE operation. It gives developers control over how database operations are processed and is especially useful when working with views. The inserted and deleted logical tables provide access to the affected records.