MSDN Documentation

SQL Triggers: Automating Database Actions

SQL Triggers are special stored procedures that automatically execute or fire in response to certain events on a particular table or view in a database. They are commonly used to maintain the integrity of data, enforce business rules, and automate complex auditing procedures.

What are SQL Triggers?

A trigger is a set of SQL statements that are invoked when an INSERT, UPDATE, or DELETE statement is executed against a table. You can think of them as an automated response mechanism for data modifications. Triggers can be defined to fire:

Key Concepts

Creating a Basic Trigger

Let's consider a scenario where we want to log changes made to an `Employees` table into an `EmployeeAudit` table.

Example Schema

Assume we have the following tables:


CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    Salary DECIMAL(10, 2)
);

CREATE TABLE EmployeeAudit (
    AuditID INT IDENTITY(1,1) PRIMARY KEY,
    EmployeeID INT,
    ActionType VARCHAR(10),
    OldSalary DECIMAL(10, 2),
    NewSalary DECIMAL(10, 2),
    ChangeDate DATETIME DEFAULT GETDATE()
);
            

Trigger for Salary Updates

This trigger will record any salary changes in the Employees table.


CREATE TRIGGER trg_EmployeeSalaryUpdate
ON Employees
AFTER UPDATE OF Salary
AS
BEGIN
    IF UPDATE(Salary)
    BEGIN
        INSERT INTO EmployeeAudit (EmployeeID, ActionType, OldSalary, NewSalary)
        SELECT
            i.EmployeeID,
            'UPDATE',
            d.Salary,
            i.Salary
        FROM
            inserted i
        INNER JOIN
            deleted d ON i.EmployeeID = d.EmployeeID
        WHERE
            i.Salary <> d.Salary; -- Only log if salary actually changed
    END
END;
                

Understanding inserted and deleted Tables

When a trigger fires, SQL Server creates two virtual, temporary tables in memory: inserted and deleted.

These tables are crucial for comparing old and new data within a trigger, allowing for detailed logging and complex business rule enforcement.

Common Trigger Use Cases

Considerations and Best Practices

Triggers are powerful tools for database management, but they should be used judiciously. Understanding their behavior and potential impact on performance is key to effective implementation.