Exforsys

SQL Server 2005 Training

  1. SQL Server 2005 - Configuring Replication
  2. SQL Server 2005 Replication Enhancements
  3. SQL Server 2005 - Mirror Server
  4. SQL Server 2005 - Introduction to Data Availability
  5. SQL Server 2005 - Backing up a Database
  6. SQL Server 2005 - Using Database Snapshots
  7. SQL Server 2005 - Disaster Recovery
  8. SQL Server 2005 - Managing Certificates
  9. SQL Server 2005 - Managing Permissions
  10. Managing SQL Server 2005 Security
  11. SQL Server 2005 - Using the Database Tuning Advisor
  12. SQL Server 2005 - Tuning a Database
  13. Maintain indexes in a SQL Server 2005 database
  14. SQL Server 2005 - Defining Indexes
  15. SQL Server 2005 - Database Backup
  16. SQL Server 2005 - Populating the Database
  17. SQL Server 2005 Configuration Manager
  18. SQL Server 2005 - Using the Sqlcmd Utility
  19. Using the SQL Management Objects
  20. Using SQL Sever Management Studio - Part 2
  21. Using SQL Sever Management Studio - Part 1
  22. SQL Server 2005 - Using Event Notifications
  23. SQL Server 2005 - Using DDL Triggers
  24. SQL Server Monitoring Tools - Server Profiler
  25. SQL Server 2005 - Testing Troubleshooting
  26. SQL Server 2005 - Upgrading from earlier versions of SQL Server
  27. SQL Server 2005 Installation - Maintenance Plan Without Using Wizard
  28. SQL Server 2005 - Unattended Installations
  29. SQL Server 2005 Installation - Maintenance Plan Using Wizard
  30. Installing a Second Copy of SQL Server 2005
  31. Planning to Install SQL Server 2005
  32. SQL Server 2005 Installation
  33. SQL server 2005 Editions
  34. SQL Server 2005 Architecture Overview
  35. SQL Server 2005 - Management studio interface Summary Page
  36. SQL Server 2005 - Server Groups
  37. SQL Server 2005 - Registered Servers
  38. SQL Server 2005 Administrative Tools
  39. Developing Client applications in SQL Server 2005
  40. SQL Server Management Objects
  41. NET CLR in SQL Server 2005
  42. Native HTTP Support in SQL Server 2005
  43. XML Data Types in SQL Server 2005
  44. Using XML in SQL Server 2005
  45. Using Notification Services in SQL Server 2005
  46. SQL Server 2005 - Service Broker
  47. Data Manipulation Language (DML) in SQL Server 2005
  48. T-SQL Enhancements in SQL Server 2005
  49. Security Features in SQL Server 2005 for the Developer
  50. SQL Server Architecture and Components
  51. SQL Server 2005 Management Studio
  52. Overview of SQL Server 2005 for the Database Developer
  53. Getting started with SQL Server 2005

Ads


Home arrow Technical Training arrow SQL Server 2005 Training

SQL Server 2005 - Using DDL Triggers

Author : Exforsys Inc.     Published on: 12th Dec 2005    |   Last Updated on: 4th Feb 2011

SQL Server 2005 - Using DDL Triggers

In this tutorial you will learn about DDL Triggers vs DML Triggers, Syntax for creating a DDL Trigger, Designing a DDL Trigger, The EVENTDATA() Function, Implementing a Database-Scoped DDL Trigger, Implementing a Server-Scoped DDL Trigger, Event Notifications vs. DDL Triggers and General Trigger Considerations.

Ads

DDL triggers fire stored procedures in response to DDL Statements which begin with CREATE, ALTER and DROP. The DDL triggers require that the DDL statements that trigger them are run and these triggers cannot be used as INSTEAD OF triggers. The DDL events that fire the DDL triggers have to be specified in T-SQL DDL syntax. System stored procedures that perform DDL like operations are not supported. The scope of the trigger is confined to the current database or current server and the scope depends on the T-SQL event being processed. These triggers can be used for Administrative tasks such as auditing and regulating database operations. DDL triggers can be used to perform the following operations

1. Prevent changes to Database schema
2. Fire events in response to changes in the database schema
3. Record changes or events in the database schema

DDL Triggers vs DML Triggers

While DML triggers operate on INSERT, UPDATE and DELETE statements to enforce business rules and data integrity checks, DDL Triggers operate on CREATE, ALTER, DROP and other DDL statements and enforce business rules that affect databases across servers. Unlike DML triggers, DDL triggers are not scoped to schemas. However, both the types of triggers are created, modified and dropped using similar T_SQL syntax. They run managed code packaged in an assembly created in the Microsoft .NET framework and uploaded in the SQL server. Moreover, like DML triggers, DDL triggers can create and use more than one trigger on the same T_SQL Statement. The DDL trigger and the statement that fires the trigger are run within the same transaction. This transaction can be rolled back from within the trigger when there is an error. If a DDL trigger is run from a batch and explicitly includes the ROLLBACK TRANSACTION statement it will cancel the whole batch. Finally both DDL and DML triggers can be nested.

Syntax for creating a DDL Trigger

Designing a DDL Trigger

Before designing a DDL trigger the Administrator must define the scope of the trigger. He must decide whether he wants the trigger to execute at the database or server level. He must also decide whether the trigger will respond to a single DDL statement or a group of related statements. Once these have been decided upon the DBA can go on to use the EVENTDATA() TSQL function for coding the response.

The EVENTDATA() Function

This function returns XML data with event time, system process ID and event type details. The EVENTDATA() function then used by the DDL trigger to determine the kind of response to the event. The DBA can then use the XQuery to retrieve information from XML data.

The following is a sample of XML data returned by the EVENTDATA() function:

The XML returned by the function will be accessed using the following code.

If single values have to be retrieved using EVENTDATA() then the query statement wil have to be used.

Implementing a Database-Scoped DDL Trigger

Database scoped DDL triggers are fired whenever events modifying database schema are executed. The CREATE TRIGGER ON DATABASE directive is used to scope the database scoped trigger. These triggers are stored within the database and fires on events with the exception of those relating to temporary tables.

Implementing a Server-Scoped DDL Trigger

A limited set of server changes trigger off Server scoped DDL Triggers. The CREATE TRIGGER ON ALL SERVER directive scopes the DDL trigger to the server. These are stored in the master database as objects. The events are related to database changes such as CREATE, ALTER, DROP or security level changes such as CREATE LOGIN, ALTER LOGIN or DROP LOGIN.

Event Notifications vs. DDL Triggers

DDL Triggers and Event Notifications both provide login mechanisms. However, the Event notification is an asynchronous event and requires the SQL Server 2005 service Broker for processing. The event notification can be located on another server and still be processed by the current instance issuing the notification. The Event notifications can also respond to trace events. Like DDL Triggers, Event notifications utilize similarly formatted xml but do not use the EVENTDATA() function.

DDL Triggers on the other hand use the EVENTDATA() function and run in the scope of the transaction and hence a transaction can be rolled back in a DDL trigger. This is not so in an Event notification.

General Trigger Considerations

As stated earlier multiple triggers can be created for each DML or DDL statement. In SQL Server 2005 if a CREATE TRIGGER FOR UPDATA is executed on a table that already has an UPDATE Trigger, an additional trigger can be created.

Recursive Triggers enable recursions to occur when the RECURSIVE TRIGGER setting is enabled using the ALTER DATABASE. Indirect recursion occurs when cascading triggers are fired and tables are updated. Direct recursion occurs when the trigger is fired to update a table directly.

Ads

When the RECURSIVE_TRIGGERS setting is disabled, direct recursions are prevented. Indirect Recursion can be disabled by setting the nested triggers server option to 0 by using sp_configure. If any one trigger is set to perform a rollback transaction all triggers will cease to be executed.

Triggers can be nested up to 32 levels maximum. If two or more triggers are created for a table, the firing of one trigger will lead to the cascading firing of other triggers. If any one trigger in the chain goes into a loop the trigger is cancelled. Nested triggers can be disabled using the sp_configure to 0. When nested triggers are set to 0, recursive triggers are also disabled.

Triggers share with stored procedures and batches, the capability of referring to tables that do not exist at compile time. This ability is called the deferred name resolution. 



 
This tutorial is part of a SQL Server 2005 Training tutorial series. Read it from the beginning and learn yourself.

SQL Server 2005 Training

  1. SQL Server 2005 - Configuring Replication
  2. SQL Server 2005 Replication Enhancements
  3. SQL Server 2005 - Mirror Server
  4. SQL Server 2005 - Introduction to Data Availability
  5. SQL Server 2005 - Backing up a Database
  6. SQL Server 2005 - Using Database Snapshots
  7. SQL Server 2005 - Disaster Recovery
  8. SQL Server 2005 - Managing Certificates
  9. SQL Server 2005 - Managing Permissions
  10. Managing SQL Server 2005 Security
  11. SQL Server 2005 - Using the Database Tuning Advisor
  12. SQL Server 2005 - Tuning a Database
  13. Maintain indexes in a SQL Server 2005 database
  14. SQL Server 2005 - Defining Indexes
  15. SQL Server 2005 - Database Backup
  16. SQL Server 2005 - Populating the Database
  17. SQL Server 2005 Configuration Manager
  18. SQL Server 2005 - Using the Sqlcmd Utility
  19. Using the SQL Management Objects
  20. Using SQL Sever Management Studio - Part 2
  21. Using SQL Sever Management Studio - Part 1
  22. SQL Server 2005 - Using Event Notifications
  23. SQL Server 2005 - Using DDL Triggers
  24. SQL Server Monitoring Tools - Server Profiler
  25. SQL Server 2005 - Testing Troubleshooting
  26. SQL Server 2005 - Upgrading from earlier versions of SQL Server
  27. SQL Server 2005 Installation - Maintenance Plan Without Using Wizard
  28. SQL Server 2005 - Unattended Installations
  29. SQL Server 2005 Installation - Maintenance Plan Using Wizard
  30. Installing a Second Copy of SQL Server 2005
  31. Planning to Install SQL Server 2005
  32. SQL Server 2005 Installation
  33. SQL server 2005 Editions
  34. SQL Server 2005 Architecture Overview
  35. SQL Server 2005 - Management studio interface Summary Page
  36. SQL Server 2005 - Server Groups
  37. SQL Server 2005 - Registered Servers
  38. SQL Server 2005 Administrative Tools
  39. Developing Client applications in SQL Server 2005
  40. SQL Server Management Objects
  41. NET CLR in SQL Server 2005
  42. Native HTTP Support in SQL Server 2005
  43. XML Data Types in SQL Server 2005
  44. Using XML in SQL Server 2005
  45. Using Notification Services in SQL Server 2005
  46. SQL Server 2005 - Service Broker
  47. Data Manipulation Language (DML) in SQL Server 2005
  48. T-SQL Enhancements in SQL Server 2005
  49. Security Features in SQL Server 2005 for the Developer
  50. SQL Server Architecture and Components
  51. SQL Server 2005 Management Studio
  52. Overview of SQL Server 2005 for the Database Developer
  53. Getting started with SQL Server 2005
 

Comments