Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

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.