Skip to content

Commit fea2914

Browse files
authored
Refresh trigger articles (124381) (#34713)
1 parent 57458f2 commit fea2914

File tree

2 files changed

+191
-178
lines changed

2 files changed

+191
-178
lines changed
Lines changed: 112 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,124 @@
11
---
22
title: "DDL Triggers"
3-
description: "DDL Triggers"
3+
description: DDL triggers fire in response to various Data Definition Language (DDL) events.
44
author: MikeRayMSFT
55
ms.author: mikeray
6-
ms.date: "03/14/2017"
6+
ms.reviewer: randolphwest
7+
ms.date: 07/16/2025
78
ms.service: sql
89
ms.topic: conceptual
910
helpviewer_keywords:
1011
- "DDL triggers, about DDL triggers"
11-
monikerRange: "=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
12+
monikerRange: "=azuresqldb-current || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current"
1213
---
13-
# DDL Triggers
14+
# DDL triggers
15+
1416
[!INCLUDE [SQL Server Azure SQL Database Azure SQL Managed Instance](../../includes/applies-to-version/sql-asdb-asdbmi.md)]
15-
DDL triggers fire in response to a variety of Data Definition Language (DDL) events. These events primarily correspond to [!INCLUDE[tsql](../../includes/tsql-md.md)] statements that start with the keywords CREATE, ALTER, DROP, GRANT, DENY, REVOKE or UPDATE STATISTICS. Certain system stored procedures that perform DDL-like operations can also fire DDL triggers.
16-
17-
Use DDL triggers when you want to do the following:
18-
19-
- Prevent certain changes to your database schema.
20-
21-
- Have something occur in the database in response to a change in your database schema.
22-
23-
- Record changes or events in the database schema.
24-
17+
18+
DDL triggers fire in response to various Data Definition Language (DDL) events. These events primarily correspond to [!INCLUDE [tsql](../../includes/tsql-md.md)] statements that start with the keywords `CREATE`, `ALTER`, `DROP`, `GRANT`, `DENY`, `REVOKE`, or `UPDATE STATISTICS`. Certain system stored procedures that perform DDL-like operations can also fire DDL triggers.
19+
20+
Use DDL triggers when you want to do the following tasks:
21+
22+
- Prevent certain changes to your database schema.
23+
- Have something occur in the database in response to a change in your database schema.
24+
- Record changes or events in the database schema.
25+
2526
> [!IMPORTANT]
26-
> Test your DDL triggers to determine their responses to system stored procedures that are run. For example, the CREATE TYPE statement and the **sp_addtype** stored procedure will both fire a DDL trigger that is created on a CREATE_TYPE event.
27-
28-
## Types of DDL Triggers
29-
### Transact-SQL DDL Trigger
30-
A special type of [!INCLUDE[tsql](../../includes/tsql-md.md)] stored procedure that executes one or more [!INCLUDE[tsql](../../includes/tsql-md.md)] statements in response to a server-scoped or database-scoped event. For example, a DDL Trigger may fire if a statement such as ALTER SERVER CONFIGURATION is executed or if a table is deleted by using DROP TABLE.
31-
32-
### CLR DDL Trigger
33-
Instead of executing a [!INCLUDE[tsql](../../includes/tsql-md.md)] stored procedure, a CLR trigger executes one or more methods written in managed code that are members of an assembly created in the .NET Framework and uploaded in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].
34-
35-
DDL triggers fire only after the DDL statements that trigger them are run. DDL triggers cannot be used as INSTEAD OF triggers. DDL triggers do not fire in response to events that affect local or global temporary tables and stored procedures.
36-
37-
DDL triggers do not create the special **inserted** and **deleted** tables.
38-
39-
The information about an event that fires a DDL trigger, and the subsequent changes caused by the trigger, is captured by using the EVENTDATA function.
40-
41-
Multiple triggers to be created for each DDL event.
42-
43-
Unlike DML triggers, DDL triggers are not scoped to schemas. Therefore, functions such as OBJECT_ID, OBJECT_NAME, OBJECTPROPERTY, and OBJECTPROPERTYEX cannot be used for querying metadata about DDL triggers. Use the catalog views instead.
44-
45-
Server-scoped DDL triggers appear in the SQL Server Management Studio Object Explorer in the **Triggers** folder. This folder is located under the **Server Objects** folder. Database-scoped DDL triggers appear in the **Database Triggers** folder. This folder is located under the **Programmability** folder of the corresponding database.
46-
27+
> Test your DDL triggers to determine their responses to system stored procedures that are run. For example, the `CREATE TYPE` statement and the `sp_addtype` stored procedure both fire a DDL trigger that is created on a `CREATE_TYPE` event.
28+
29+
## Types of DDL trigger
30+
31+
### Transact-SQL trigger
32+
33+
A special type of [!INCLUDE [tsql](../../includes/tsql-md.md)] stored procedure that executes one or more [!INCLUDE [tsql](../../includes/tsql-md.md)] statements in response to a server-scoped or database-scoped event. For example, a DDL trigger might fire if a statement such as `ALTER SERVER CONFIGURATION` is executed or if a table is deleted by using `DROP TABLE`.
34+
35+
### CLR trigger
36+
37+
Instead of executing a [!INCLUDE [tsql](../../includes/tsql-md.md)] stored procedure, a common language runtime (CLR) trigger executes one or more methods written in managed code that are members of an assembly created in the .NET Framework and uploaded in [!INCLUDE [ssNoVersion](../../includes/ssnoversion-md.md)].
38+
39+
DDL triggers fire only after the DDL statements that trigger them are run. DDL triggers can't be used as `INSTEAD OF` triggers. DDL triggers don't fire in response to events that affect local or global temporary tables and stored procedures.
40+
41+
DDL triggers don't create the special `inserted` and `deleted` tables.
42+
43+
The information about an event that fires a DDL trigger, and the subsequent changes caused by the trigger, is captured by using the `EVENTDATA` function.
44+
45+
Multiple triggers to be created for each DDL event.
46+
47+
Unlike Data Manipulation Language (DML) triggers, DDL triggers aren't scoped to schemas. Therefore, functions such as `OBJECT_ID`, `OBJECT_NAME`, `OBJECTPROPERTY`, and `OBJECTPROPERTYEX` can't be used for querying metadata about DDL triggers. Use the catalog views instead.
48+
49+
Server-scoped DDL triggers appear in the SQL Server Management Studio Object Explorer in the **Triggers** folder. This folder is located under the **Server Objects** folder. Database-scoped DDL triggers appear in the **Database Triggers** folder. This folder is located under the **Programmability** folder of the corresponding database.
50+
4751
> [!IMPORTANT]
48-
> Malicious code inside triggers can run under escalated privileges. For more information about how to help reduce this threat, see [Manage Trigger Security](../../relational-databases/triggers/manage-trigger-security.md).
49-
50-
## DDL Trigger Scope
51-
DDL triggers can fire in response to a [!INCLUDE[tsql](../../includes/tsql-md.md)] event processed in the current database, or on the current server. The scope of the trigger depends on the event. For example, a DDL trigger created to fire in response to a CREATE_TABLE event can do so whenever a CREATE_TABLE event occurs in the database, or on the server instance. A DDL trigger created to fire in response to a CREATE_LOGIN event can do so only when a CREATE_LOGIN event occurs in the server instance.
52-
53-
In the following example, DDL trigger `safety` will fire whenever a `DROP_TABLE` or `ALTER_TABLE` event occurs in the database.
54-
55-
```
56-
CREATE TRIGGER safety
57-
ON DATABASE
58-
FOR DROP_TABLE, ALTER_TABLE
59-
AS
60-
PRINT 'You must disable Trigger "safety" to drop or alter tables!'
61-
ROLLBACK;
62-
```
63-
64-
In the following example, a DDL trigger prints a message if any `CREATE_DATABASE` event occurs on the current server instance. The example uses the `EVENTDATA` function to retrieve the text of the corresponding [!INCLUDE[tsql](../../includes/tsql-md.md)] statement. For more information about how to use EVENTDATA with DDL triggers, see [Use the EVENTDATA Function](../../relational-databases/triggers/use-the-eventdata-function.md).
65-
66-
```
67-
IF EXISTS (SELECT * FROM sys.server_triggers
68-
WHERE name = 'ddl_trig_database')
69-
DROP TRIGGER ddl_trig_database
70-
ON ALL SERVER;
71-
GO
72-
CREATE TRIGGER ddl_trig_database
73-
ON ALL SERVER
74-
FOR CREATE_DATABASE
75-
AS
76-
PRINT 'Database Created.'
77-
SELECT EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)')
78-
GO
79-
DROP TRIGGER ddl_trig_database
80-
ON ALL SERVER;
81-
GO
82-
83-
```
84-
85-
The lists that map the [!INCLUDE[tsql](../../includes/tsql-md.md)] statements to the scopes that can be specified for them are available through the links provided in the section "Selecting a Particular DDL Statement to Fire a DDL Trigger," later in this topic.
86-
87-
Database-scoped DDL triggers are stored as objects in the database in which they are created. DDL triggers can be created in the **master** database and behave just like those created in user-designed databases. You can obtain information about DDL triggers by querying the **sys.triggers** catalog view. You can query **sys.triggers** within the database context in which the triggers are created or by specifying the database name as an identifier, such as **master.sys.triggers**.
88-
89-
Server-scoped DDL triggers are stored as objects in the **master** database. However, you can obtain information about server-scoped DDL triggers by querying the **sys.server_triggers** catalog view in any database context.
90-
91-
## Specifying a Transact-SQL Statement or Group of Statements
92-
93-
### Selecting a Particular DDL Statement to Fire a DDL Trigger
94-
DDL triggers can be designed to fire after one or more particular [!INCLUDE[tsql](../../includes/tsql-md.md)] statements are run. In the previous example, trigger `safety` fires after any `DROP_TABLE` or `ALTER_TABLE` event. For lists of the [!INCLUDE[tsql](../../includes/tsql-md.md)] statements that can be specified to fire a DDL trigger, and the scope at which the trigger can fire, see [DDL Events](../../relational-databases/triggers/ddl-events.md).
95-
96-
### Selecting a Predefined Group of DDL Statements to Fire a DDL Trigger
97-
A DDL trigger can fire after execution of any [!INCLUDE[tsql](../../includes/tsql-md.md)] event that belongs to a predefined grouping of similar events. For example, if you want a DDL trigger to fire after any CREATE TABLE, ALTER TABLE, or DROP TABLE statement is run, you can specify FOR DDL_TABLE_EVENTS in the CREATE TRIGGER statement. After CREATE TRIGGER is run, the events that are covered by an event group are added to the **sys.trigger_events** catalog view.
98-
99-
In [!INCLUDE[ssVersion2005](../../includes/ssversion2005-md.md)], if a trigger is created on an event group, **sys.trigger_events** does not include information about the event group, **sys.trigger_events** includes information only about the individual events covered by that group. In [!INCLUDE[sql2008-md](../../includes/sql2008-md.md)] and higher, **sys.trigger_events** persists metadata about the event group on which the triggers is created, and also about the individual events that the event group covers. Therefore, changes to the events that are covered by event groups in [!INCLUDE[sql2008-md](../../includes/sql2008-md.md)] and higher do not apply to DDL triggers that are created on those event groups in [!INCLUDE[ssVersion2005](../../includes/ssversion2005-md.md)].
100-
101-
For a list of the predefined groups of DDL statements that are available for DDL triggers, the particular statements the event groups cover, and the scopes at which these event groups can be programmed, see [DDL Event Groups](../../relational-databases/triggers/ddl-event-groups.md).
102-
103-
## Related Tasks
104-
105-
|Task|Topic|
106-
|----------|-----------|
107-
|Describes how to create, modify, delete or disable DDL triggers.|[Implement DDL Triggers](../../relational-databases/triggers/implement-ddl-triggers.md)|
108-
|Describes how to create a CLR DDL trigger.|[Create CLR Triggers](../../relational-databases/triggers/create-clr-triggers.md)|
109-
|Describes how to return information about DDL triggers.|[Get Information About DDL Triggers](../../relational-databases/triggers/get-information-about-ddl-triggers.md)|
110-
|Describes how to return information about an event that fires a DDL trigger by using the EVENTDATA function.|[Use the EVENTDATA Function](../../relational-databases/triggers/use-the-eventdata-function.md)|
111-
|Describes how to manage trigger security.|[Manage Trigger Security](../../relational-databases/triggers/manage-trigger-security.md)|
112-
113-
## See Also
114-
[DML Triggers](../../relational-databases/triggers/dml-triggers.md)
115-
[Logon Triggers](../../relational-databases/triggers/logon-triggers.md)
116-
[CREATE TRIGGER (Transact-SQL)](../../t-sql/statements/create-trigger-transact-sql.md)
117-
[EVENTDATA (Transact-SQL)](../../t-sql/functions/eventdata-transact-sql.md)
118-
52+
> Malicious code inside triggers can run under escalated privileges. For more information about how to help reduce this threat, see [Manage trigger security](manage-trigger-security.md).
53+
54+
## DDL trigger scope
55+
56+
DDL triggers can fire in response to a [!INCLUDE [tsql](../../includes/tsql-md.md)] event processed in the current database, or on the current server. The scope of the trigger depends on the event. For example, a DDL trigger created to fire in response to a `CREATE_TABLE` event can do so whenever a `CREATE_TABLE` event occurs in the database, or on the server instance. A DDL trigger created to fire in response to a `CREATE_LOGIN` event can do so only when a `CREATE_LOGIN` event occurs in the server instance.
57+
58+
In the following example, DDL trigger `safety` fires whenever a `DROP_TABLE` or `ALTER_TABLE` event occurs in the database.
59+
60+
```sql
61+
CREATE TRIGGER safety
62+
ON DATABASE
63+
FOR DROP_TABLE, ALTER_TABLE
64+
AS PRINT 'You must disable trigger "safety" to drop or alter tables!';
65+
ROLLBACK;
66+
```
67+
68+
In the following example, a DDL trigger prints a message if any `CREATE_DATABASE` event occurs on the current server instance. The example uses the `EVENTDATA` function to retrieve the text of the corresponding [!INCLUDE [tsql](../../includes/tsql-md.md)] statement. For more information about how to use `EVENTDATA` with DDL triggers, see [Use the EVENTDATA Function](use-the-eventdata-function.md).
69+
70+
```sql
71+
IF EXISTS (SELECT *
72+
FROM sys.server_triggers
73+
WHERE name = 'ddl_trig_database')
74+
DROP TRIGGER ddl_trig_database
75+
ON ALL SERVER;
76+
GO
77+
78+
CREATE TRIGGER ddl_trig_database
79+
ON ALL SERVER
80+
FOR CREATE_DATABASE
81+
AS PRINT 'Database Created.';
82+
SELECT EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]', 'nvarchar(max)');
83+
GO
84+
85+
DROP TRIGGER ddl_trig_database
86+
ON ALL SERVER;
87+
```
88+
89+
The lists that map the [!INCLUDE [tsql](../../includes/tsql-md.md)] statements to the scopes that can be specified for them are available through the links provided in the section [Select a particular DDL statement to fire a DDL trigger](#select-a-particular-ddl-statement-to-fire-a-ddl-trigger) later in this article.
90+
91+
Database-scoped DDL triggers are stored as objects in the database in which they're created. DDL triggers can be created in the `master` database and behave just like those created in user-designed databases. You can obtain information about DDL triggers by querying the `sys.triggers` catalog view. You can query `sys.triggers` within the database context in which the triggers are created or by specifying the database name as an identifier, such as `master.sys.triggers`.
92+
93+
Server-scoped DDL triggers are stored as objects in the `master` database. However, you can obtain information about server-scoped DDL triggers by querying the `sys.server_triggers` catalog view in any database context.
94+
95+
## Specify a Transact-SQL statement or group of statements
96+
97+
### Select a particular DDL statement to fire a DDL trigger
98+
99+
DDL triggers can be designed to fire after one or more particular [!INCLUDE [tsql](../../includes/tsql-md.md)] statements are run. In the previous example, trigger `safety` fires after any `DROP_TABLE` or `ALTER_TABLE` event. For lists of the [!INCLUDE [tsql](../../includes/tsql-md.md)] statements that can be specified to fire a DDL trigger, and the scope at which the trigger can fire, see [DDL Events](ddl-events.md).
100+
101+
### Select a predefined group of DDL statements to fire a DDL trigger
102+
103+
A DDL trigger can fire after execution of any [!INCLUDE [tsql](../../includes/tsql-md.md)] event that belongs to a predefined grouping of similar events. For example, if you want a DDL trigger to fire after any `CREATE TABLE`, `ALTER TABLE`, or `DROP TABLE` statement is run, you can specify `FOR DDL_TABLE_EVENTS` in the `CREATE TRIGGER` statement. After `CREATE TRIGGER` is run, the events that are covered by an event group are added to the `sys.trigger_events` catalog view.
104+
105+
In [!INCLUDE [ssVersion2005](../../includes/ssversion2005-md.md)], if a trigger is created on an event group, `sys.trigger_events` doesn't include information about the event group, `sys.trigger_events` includes information only about the individual events covered by that group. `sys.trigger_events` persists metadata about the event group on which the trigger is created, and also about the individual events that the event group covers. Therefore, changes to the events that are covered by event groups don't apply to DDL triggers in recent versions of [!INCLUDE [ssnoversion-md](../../includes/ssnoversion-md.md)] that are created on those event groups in [!INCLUDE [ssVersion2005](../../includes/ssversion2005-md.md)].
106+
107+
For a list of the predefined groups of DDL statements that are available for DDL triggers, the particular statements the event groups cover, and the scopes at which these event groups can be programmed, see [DDL Event Groups](ddl-event-groups.md).
108+
109+
## Related tasks
110+
111+
| Task | Article |
112+
| --- | --- |
113+
| Describes how to create, modify, delete, or disable DDL triggers. | [Implement DDL Triggers](implement-ddl-triggers.md) |
114+
| Describes how to create a CLR DDL trigger. | [Create CLR triggers](create-clr-triggers.md) |
115+
| Describes how to return information about DDL triggers. | [Get Information About DDL Triggers](get-information-about-ddl-triggers.md) |
116+
| Describes how to return information about an event that fires a DDL trigger by using the `EVENTDATA` function. | [Use the EVENTDATA Function](use-the-eventdata-function.md) |
117+
| Describes how to manage trigger security. | [Manage trigger security](manage-trigger-security.md) |
118+
119+
## Related content
120+
121+
- [DML Triggers](dml-triggers.md)
122+
- [Logon triggers](logon-triggers.md)
123+
- [CREATE TRIGGER (Transact-SQL)](../../t-sql/statements/create-trigger-transact-sql.md)
124+
- [EVENTDATA (Transact-SQL)](../../t-sql/functions/eventdata-transact-sql.md)

0 commit comments

Comments
 (0)