| Lesson 10 | Testing triggers |
| Objective | Describe how to test your Triggers in SQL Server 2025 |
Testing triggers is an essential step in verifying that business rules are enforced correctly in a relational database system. Because triggers execute automatically when a data modification occurs, developers must ensure that the trigger behaves correctly for both valid and invalid data changes.
In SQL Server 2025, testing a trigger typically involves executing Transact-SQL statements that simulate the conditions the trigger is designed to handle. These tests intentionally attempt both valid and invalid operations so that the database engine’s response can be observed.
Earlier in this module we created a trigger named trgSalary. This trigger enforces a business rule
requiring manager authorization when an employee salary exceeds $150,000. Because trgSalary is an
INSERT trigger, it is tested by issuing INSERT statements that represent different
business scenarios.
The goal of testing is simple: verify that the trigger allows valid operations and blocks operations that violate the business rule.
The following examples simulate three possible scenarios that the trigger must handle. These statements represent a simple test suite used to confirm that the trigger behaves as expected.
-- Test 1: Normal salary (should succeed)
INSERT INTO dbo.Employees (EmployeeID, Salary)
VALUES (1, 100000);
GO
-- Test 2: High salary without approval
-- This should fail if the trigger enforces the rule
INSERT INTO dbo.Employees (EmployeeID, Salary)
VALUES (2, 200000);
GO
-- Test 3: High salary with manager approval
-- This should succeed
INSERT INTO dbo.Employees (EmployeeID, Salary, ApprovalID)
VALUES (3, 200000, 52);
GO
These tests demonstrate the typical approach used when validating trigger logic:
If the trigger is functioning correctly, the second test should generate an error and roll back the transaction, while the first and third tests should succeed.
Although the previous example tests an INSERT trigger, developers should also verify behavior
for other data modification operations. In many real-world systems, triggers are written for
UPDATE or DELETE operations as well.
For example, after validating the INSERT logic, you might issue additional tests such as:
-- Test UPDATE behavior
UPDATE dbo.Employees
SET Salary = 175000
WHERE EmployeeID = 1;
If the trigger is defined only for INSERT operations, the update will execute normally because the trigger does not fire for UPDATE events.
Similarly, a DELETE statement should not invoke an INSERT trigger:
DELETE FROM dbo.Employees
WHERE EmployeeID = 1;
Testing multiple operations helps confirm that the trigger fires only for the events it was designed to handle.
In production environments, developers often test triggers inside explicit transactions so that test data can be safely rolled back after validation.
BEGIN TRANSACTION;
INSERT INTO dbo.Employees (EmployeeID, Salary)
VALUES (99, 200000);
ROLLBACK TRANSACTION;
This technique ensures that test records do not permanently alter the database while still allowing developers to observe how the trigger behaves.
DML triggers are frequently used to enforce business rules and maintain data integrity. However, they are only one of several tools available in SQL Server for protecting relational data.
SQL Server supports Declarative Referential Integrity (DRI) through constraints such as:
PRIMARY KEYFOREIGN KEYCHECKUNIQUEThese constraints enforce rules directly within the relational model and should generally be used whenever possible. Triggers are most appropriate when a rule cannot be expressed using standard constraints, such as when logic spans multiple tables or requires conditional evaluation.
Understanding how triggers interact with constraints is also important when testing them.
Because of this execution order, testing should include scenarios that verify constraint interactions with trigger logic.
SQL Server allows multiple AFTER triggers to exist on the same table. When this occurs, execution order can
be controlled using the stored procedure sp_settriggerorder.
Only one trigger can be designated as the first trigger and one as the last trigger for a given operation
(INSERT, UPDATE, or DELETE). Any additional triggers are executed in
an unspecified order.
Testing becomes especially important when multiple triggers interact, since complex trigger chains can produce unintended behavior if they are not validated carefully.
In the next lesson, the information covered in this module will be reviewed and summarized.