Known limitations of triggers

Building and Optimizing Triggers in SQL Server

Florin Angelescu

Instructor

Advantages of triggers

  • Used for database integrity
  • Enforce business rules directly in the database
  • Control on which statements are allowed in a database
  • Implementation of complex business logic triggered by a single event
  • Simple way to audit databases and user actions
Building and Optimizing Triggers in SQL Server

Disadvantages of triggers

  • Difficult to view and detect
  • Invisible to client applications or when debugging code
  • Hard to follow their logic when troubleshooting
  • Can become an overhead on the server and make it run slower
Building and Optimizing Triggers in SQL Server

Finding server-level triggers

SELECT * FROM sys.server_triggers;
| name                 | parent_class_desc | create_date | is_disabled |...|
|----------------------|-------------------|-------------|-------------|---|
| DenyNewDatabases     | SERVER            | 2019-01-22  | 0           |...|
| DenyNewLinkedServers | SERVER            | 2019-01-22  | 1           |...|
| DenyDatabaseDelete   | SERVER            | 2019-01-22  | 1           |...|
Building and Optimizing Triggers in SQL Server

Finding database and table triggers

SELECT * FROM sys.triggers;
| name                   | parent_class_desc | create_date | is_disabled | is_instead_of_trigger |
|----------------------|-------------------|-------------|---------------|-----------------------|
| TrackRetiredProducts   | OBJECT_OR_COLUMN  | 2019-04-19  | 0           | 0                     |
| PreventOrdersUpdate    | OBJECT_OR_COLUMN  | 2019-04-22  | 0           | 1                     |
| TrackDatabaseTables    | DATABASE          | 2019-04-23  | 0           | 0                     |
| KeepCanceledOrders     | OBJECT_OR_COLUMN  | 2019-04-27  | 0           | 0                     |
| DiscountsPreventDelete | OBJECT_OR_COLUMN  | 2019-04-27  | 0           | 1                     |
| PreventNewDiscounts    | OBJECT_OR_COLUMN  | 2019-04-27  | 0           | 1                     |
| PreventTableDeletion   | DATABASE          | 2019-04-27  | 0           | 0                     |
Building and Optimizing Triggers in SQL Server

Viewing a trigger definition (option 1)

View Trigger Defitinion from SSMS

CREATE TRIGGER PreventOrdersUpdate
ON Orders
INSTEAD OF UPDATE
AS
RAISERROR ('Updates on "Orders" table
            are not permitted.
            Place a new order
            to add new products.', 16, 1);
Building and Optimizing Triggers in SQL Server

Viewing a trigger definition (option 2)

SELECT definition
FROM sys.sql_modules
WHERE object_id = OBJECT_ID ('PreventOrdersUpdate');
| definition                                                   |
|--------------------------------------------------------------|
| CREATE TRIGGER PreventOrdersUpdate                           |
| ON Orders                                                    |
| INSTEAD OF UPDATE                                            |
| AS                                                           |
| RAISERROR ('Updates on "Orders" table are not permitted.     |
|             Place a new order to add new products.', 16, 1); |
Building and Optimizing Triggers in SQL Server

Viewing a trigger definition (option 3)

SELECT OBJECT_DEFINITION (OBJECT_ID ('PreventOrdersUpdate'));
| (No column name)                                             |
|--------------------------------------------------------------|
| CREATE TRIGGER PreventOrdersUpdate                           |
| ON Orders                                                    |
| INSTEAD OF UPDATE                                            |
| AS                                                           |
| RAISERROR ('Updates on "Orders" table are not permitted.     |
|             Place a new order to add new products.', 16, 1); |
Building and Optimizing Triggers in SQL Server

Viewing a trigger definition (option 4)

EXECUTE sp_helptext @objname = 'PreventOrdersUpdate';
| Text                                                         |
|--------------------------------------------------------------|
| CREATE TRIGGER PreventOrdersUpdate                           |
| ON Orders                                                    |
| INSTEAD OF UPDATE                                            |
| AS                                                           |
| RAISERROR ('Updates on "Orders" table are not permitted.     |
|             Place a new order to add new products.', 16, 1); |
Building and Optimizing Triggers in SQL Server

Triggers best practice

Tips:

  • well-documented database design
  • simple logic in trigger design
  • avoid overusing triggers
Building and Optimizing Triggers in SQL Server

Let's practice!

Building and Optimizing Triggers in SQL Server

Preparing Video For Download...