In this article

Using the EVENTDATA Function

In this article

Information about an event that fires a DDL trigger is captured by using the EVENTDATA function. This function returns an xml value. The XML schema includes information about the following:

The time of the event.

The System Process ID (SPID) of the connection when the trigger executed.

The type of event that fired the trigger.

Depending on the event type, the schema then includes additional information such as the database in which the event occurred, the object against which the event occurred, and the Transact-SQL statement of the event. For more information, see EVENTDATA (Transact-SQL).

For example, the following DDL trigger is created in the AdventureWorks sample database:

The EVENTDATA() statement in the DDL trigger captures the text of the CREATE TABLEstatement that is not allowed. This is achieved by using an XQuery statement against the xml data that is generated by EVENTDATA and retrieving the <CommandText> element. For more information, see XQuery Against the xml Data Type.

Warning

EVENTDATA captures the data of CREATE_SCHEMA events as well as the <schema_element> of the corresponding CREATE SCHEMA definition, if any exists. Additionally, EVENTDATA recognizes the <schema_element> definition as a separate event. Therefore, a DDL trigger created on both a CREATE_SCHEMA event, and an event represented by the <schema_element> of the CREATE SCHEMA definition, may return the same event data twice, such as the TSQLCommand data. For example, consider a DDL trigger that is created on both the CREATE_SCHEMA and CREATE_TABLE events and the following batch is run: CREATE SCHEMA sCREATE TABLE t1 (col1 int) If the application retrieves the TSQLCommand data of the CREATE_TABLE event, be aware that this data may appear two times: once when the CREATE_SCHEMA event occurs, and again when the CREATE_TABLE event occurs. Avoid creating DDL triggers on both the CREATE_SCHEMA events and the <schema_element> texts of any corresponding CREATE SCHEMA definitions, or build logic into your application so that the same event is not processed twice.

Example

You can use the EVENTDATA function to create a log of events. In the following example, a table is created to store event information. A DDL trigger is then created on the current database that populates the table with the following information whenever any database-level DDL event occurs:

The time of the event (using the GETDATE function).

The database user against whose session the event occurred (using the CURRENT_USER function).

The type of the event.

The Transact-SQL statement that comprised the event.

Again, the last two items are captured by using XQuery against the xml data that is generated by EVENTDATA.

To return event data, we recommend that you use the XQuery value() method instead of the query() method. The query() method returns XML and ampersand-escaped carriage return and line-feed (CRLF) instances in the output, while the value() method renders CRLF instances invisible in the output.

A similar DDL trigger example is provided with the AdventureWorks sample database. To obtain the example, locate the Database Triggers folder by using SQL Server Management Studio. This folder is located under the Programmability folder of the AdventureWorks database. Right-click ddlDatabseTriggerLog and select Script Database Trigger as. By default, DDL trigger ddlDatabseTriggerLog is disabled.