SQL-Server Triggers  «Prev  Next»

Lesson 10 Testing triggers
Objective Describe how to test your triggers.

Testing SQL-Server Triggers

You might think that testing your triggers is difficult. However, this could not be farther from the truth. To test a trigger, you simply issue a Transact-SQL statement that violates the rules of your trigger, and see how SQL Server reacts.
Earlier in this module, we modified the trgSalary trigger so that authorization is now required for any salary greater than $150,000. Because trgSalary is an INSERT trigger, it can be tested with the INSERT Transact-SQL statement, making sure that it covers all cases relating to the purpose of the trigger.
The following series of images below examines these statements:

Issue Transact SQL Statement

The statement inserts a salary that is less than $150,000 and should be allowed.
1) The statement insert a salary that is less than $150,000 and should be allowed.

This statement inserts a salary that is higher than $150,000, without an approal. This statement should not be allowed.
2) This statement inserts a salary that is higher than $150,000, without an approal. This statement should not be allowed.

This statement inserts a salary that is higher than $150,000, with approval and should be allowed.
3) This statement inserts a salary that is higher than $150,000, with approval and should be allowed.


After running these tests, substitute each using either the UPDATE or the DELETE Transact-SQL statement. These statements should be allowed, because our trigger is only an INSERT trigger.
In the next lesson, the information covered in this module will be reviewed.

DML Triggers

DML triggers are frequently used for enforcing business rules and data integrity [1]. SQL Server provides declarative referential integrity through the ALTER TABLE and CREATE TABLE statements. However, DRI does not provide cross-database referential integrity. Referential integrity refers to the rules about the relationships between the primary and foreign keys of tables. To enforce referential integrity, use the PRIMARY KEY and FOREIGN KEY constraints in ALTER TABLE and CREATE TABLE. If constraints exist on the trigger table, they are checked after the INSTEAD OF trigger execution and before the AFTER trigger execution. If the constraints are violated, the INSTEAD OF trigger actions are rolled back and the AFTER trigger is not fired.
The first and last AFTER triggers to be executed on a table can be specified by using sp_settriggerorder. Only one first and one last AFTER trigger for each INSERT, UPDATE, and DELETE operation can be specified on a table. If there are other AFTER triggers on the same table, they are randomly executed. If an ALTER TRIGGER statement changes a first or last trigger, the first or last attribute set on the modified trigger is dropped, and the order value must be reset by using sp_settriggerorder. An AFTER trigger is executed only after the triggering SQL statement has executed successfully. This successful execution includes all referential cascade actions and constraint checks associated with the object updated or deleted. An AFTER trigger will not recursively fire an INSTEAD OF trigger on the same table.
If an INSTEAD OF trigger defined on a table executes a statement against the table that would ordinarily fire the INSTEAD OF trigger again, the trigger is not called recursively. Instead, the statement is processed as if the table had no INSTEAD OF trigger and starts the chain of constraint operations and AFTER trigger executions. For example, if a trigger is defined as an INSTEAD OF INSERT trigger for a table, and the trigger executes an INSERT statement on the same table, the INSERT statement executed by the INSTEAD OF trigger does not call the trigger again. The INSERT executed by the trigger starts the process of performing constraint actions and firing any AFTER INSERT triggers defined for the table. If an INSTEAD OF trigger defined on a view executes a statement against the view that would ordinarily fire the INSTEAD OF trigger again, it is not called recursively. Instead, the statement is resolved as modifications against the base tables underlying the view. In this case, the view definition must meet all the restrictions for an updatable view. For a definition of updatable views, see Modify Data Through a View.
For example, if a trigger is defined as an INSTEAD OF UPDATE trigger for a view, and the trigger executes an UPDATE statement referencing the same view, the UPDATE statement executed by the INSTEAD OF trigger does not call the trigger again. The UPDATE executed by the trigger is processed against the view as if the view did not have an INSTEAD OF trigger. The columns changed by the UPDATE must be resolved to a single base table. Each modification to an underlying base table starts the chain of applying constraints and firing AFTER triggers defined for the table.

[1] data integrity: Data integrity is the maintenance of, and the assurance of the accuracy and consistency of data over its entire life-cycle, and is a critical aspect to the design, implementation and usage of any system which stores, processes, or retrieves data.

SEMrush Software