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

AFTER UPDATE Trigger

Learn how AFTER UPDATE Triggers work in SQL Server, how to create them and how to use the inserted and deleted logical tables.

What is an AFTER UPDATE Trigger?

An AFTER UPDATE Trigger is a special SQL Server database object that automatically executes after a successful UPDATE operation on a table.

It is useful when we want to perform an automatic action whenever existing records are modified.

  • It executes after an UPDATE operation.
  • It can be used for auditing changes.
  • It can store old and new values.
  • It can maintain history or log tables.
  • It can perform automatic database operations.

Basic Syntax

CREATE TRIGGER TriggerName
ON TableName
AFTER UPDATE
AS
BEGIN

    -- Trigger logic

END;

Example Table

Let's assume we have a 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
);

Create an Audit Table

We can create a separate table to store information whenever a student record is updated.

CREATE TABLE StudentAudit
(
    AuditId INT IDENTITY(1,1) PRIMARY KEY,
    StudentId INT,
    OldCity VARCHAR(50),
    NewCity VARCHAR(50),
    OldAge INT,
    NewAge INT,
    ChangedDate DATETIME
);

Creating an AFTER UPDATE Trigger

Now we create a trigger that automatically records the old and new values whenever a student is updated.

CREATE TRIGGER trg_StudentAfterUpdate
ON Students
AFTER UPDATE
AS
BEGIN

    INSERT INTO StudentAudit
    (
        StudentId,
        OldCity,
        NewCity,
        OldAge,
        NewAge,
        ChangedDate
    )
    SELECT
        d.StudentId,
        d.City,
        i.City,
        d.Age,
        i.Age,
        GETDATE()
    FROM deleted d
    INNER JOIN inserted i
        ON d.StudentId = i.StudentId;

END;

inserted and deleted Logical Tables

SQL Server provides two special logical tables inside an UPDATE trigger:

Logical Table Contains
deleted Old values before the UPDATE
inserted New values after the UPDATE

Performing an UPDATE

Now update the student's city and age.

UPDATE Students
SET
    City = 'Mumbai',
    Age = 22
WHERE StudentId = 101;

What Happens During UPDATE?

Suppose the old record was:

StudentId = 101
City = Pune
Age = 21

After the UPDATE:

StudentId = 101
City = Mumbai
Age = 22

Inside the trigger:

  • deleted contains Pune and 21.
  • inserted contains Mumbai and 22.

Check the Audit Record

SELECT *
FROM StudentAudit;

The audit table will contain the old and new values generated automatically by the trigger.

AFTER UPDATE Trigger and Multiple Rows

A trigger can execute for an UPDATE statement that modifies multiple rows.

UPDATE Students
SET City = 'Pune'
WHERE Age > 20;

Therefore, trigger logic should be written using set-based operations rather than assuming that only one row is updated.

Important
  • AFTER UPDATE triggers execute after the UPDATE operation.
  • deleted contains the old values.
  • inserted contains the new values.
  • A single UPDATE statement can affect multiple rows.
  • Always design trigger logic to handle multiple rows.
Summary

An AFTER UPDATE Trigger automatically executes after a successful UPDATE operation. It is commonly used for auditing, maintaining history, tracking changes and performing automatic database operations. The deleted logical table contains the old values, while the inserted logical table contains the new values.