Understanding DDL Triggers in SQL Server 2005



DDL triggers are a new feature in SQL Server 2005. DDL triggers are similar to DML triggers.DML triggers fires before or after issuing the UPDATE, DELETE or INSERT commands. Similarly the DDL triggers fire after the issue of CREATE, DROP or ALTER commands.

DDL Triggers can be used to:

  1. Audit the DDL (Data Definition Language) events
  2. Prevent changes happening to the database.

DDL triggers cannot be used as INSTEAD OF triggers. They fire after the issue of DDL commands.

TSQL Syntax:

CREATE TRIGGER trigger_name 
[WITH <ddl_trigger_option> [ ,...n ] ]
{FOR | AFTER} { event_type | event_group } [ ,...n ]
AS { sql_statement [ ; ] [ ...n ] | EXTERNAL NAME < method specifier > [ ; ] }
<ddl_trigger_option> ::=
 [EXECUTE AS Clause]


The DDL triggers have to be creating defining the scope of that trigger.DDL trigger fires in response to an event which has occurred in the current database or in the current server. So the scope of the DDL trigger is Database or Server.

The Database level events that can be audited are:

  1. DDL Table events: Create table, Alter table, Drop table
  2. DDL view events : Create view, Alter view, Drop view
  3. DDL trigger events :Create trigger, Drop trigger, Alter trigger
  4. DDL synonym events: Create synonym, drop synonym
  5. DDL Index events: Create index, Alter index, Drop Index
  6. DDL Database level security events:
    • Create User, Drop user, Alter user
    • Create role, Drop role, Alter role
    • Create application role, Drop application role, Alter Application role
    • Create Schema, Drop Schema, Alter Schema
    • Grant database access, Revoke database access, Deny Database access
  7. DDL Service broker events:
    • Create Message type, Alter Message type, Drop Message type
    • Create contract, Drop contract, Alter contract
    • Create Service, Alter service, Drop Service
    • Create route, Drop route, Alter route

The server level events that can be audited are

  1. Create Database, Drop Database
  2. Create Login, Drop Login, Alter Login

Event type or Event group: The trigger can be created for a specific events or a group of events.


The text of the trigger can be encrypted using the "WITH ENCRYPTION" clause in the DDL trigger syntax.

Execute As

It specifies the security context in which the trigger will be executed.



Create trigger test_ddl_trigger on ALL Server for DDL_LOGIN_Events as
Print 'You can create Logins in this server.You do not have sufficient permissions'


Create trigger test_ddl_dbtrigger on Database for Create_table as
Print 'Cannot create table due to insufficient space'

Querying about triggers

The information about the database scoped triggers can be obtained by querying the sys.triggers table. Database scoped triggers are stored in the database in which they are created. So information about them can be queried by appending the database name with sys.triggers. (Adventureworks.sys.triggers).

The information about the server scoped triggers can be obtained by querying the sys.server_triggers.

Eventdata Function

The DML triggers stores the new values in the INSERTED table and the old values in DELETED table.DDL trigger do not store in the inserted and deleted tables. The information about an event which fires the DDL trigger is available through the EVENTDATA function.

From the eventdata function,we can obtain information about the event type,the database name in which the trigger was fired,the T-SQL command text of the event that caused the trigger to fire.The output of eventdata function is of xml dataype. The eventdata function will return data only when it is referenced from inside the DDL trigger.It does not return data if it is queried by other routines.

For example, I have created trig_event_data that prevents the user from creating tables in the database.

Create TRIGGER trig_event_data 
 select eventdata()
 RAISERROR ('New tables cannot be created in this database.', 16, 1) 

To test the trigger, create the table "test5"

Create table test5(employee_id int,name_emp varchar(50))

The eventdata function generated the results as follows:

 <SetOptions ANSI_NULLS="ON"
 <CommandText>create table test5(employee_id int,name_emp varchar(50))

If the user wants only the TSQL command which caused the

trigger to fire,replace the "Select.eventdata()" by



We reach to the value of CommandText node by traversing /Event_Instance/TSQLCommand.The CommandText is the second sub-node within the TSQLCommand node.So 1 in the square brackets(starting with zero). The function can be modified to insert all the create table events into a log table. First create a table which will maintain a log of the create table events

USE AdventureWorks;


CREATE TABLE create_table_log ( create_time datetime , DB_User nvarchar(100) , Event_type nvarchar(100) , TSQL nvarchar(2000)); GO

Second,create the trigger and insert the created time,user,event type and the TSQL command of the event type into the "CREATE_TABLE_LOG" table.

create trigger trig_create_table on database for create_table
Declare @data xml
set @data=Eventdata()
Insert into create_table_log values

3)Test the trigger by issuing the command

createtable test6(ename
varchar(50),empid int,designation varchar(50),Band varchar(50),Salary int)

A record is inserted corresponding to the table "test6"

Thus DDL triggers is a powerful way of auditing the database

and the server for DDL operations.


4.45 (11)




4.45 (11)