Building and Optimizing Triggers in SQL Server
Florin Angelescu
Instructor
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 |...|
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 |
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);
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); |
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); |
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); |
Tips:
Building and Optimizing Triggers in SQL Server