Read + Write + Report
Home | Start a blog | About Orble | FAQ | Sites | Writers | Advertise | My Orble | Login

Using the EventData() Function with DDL triggers in SQL Server 2005

August 29th 2008 04:47
Using the EventData() Function with DDL triggers in SQL Server 2005


Problem
In the Auditing DDL (Create, Alter, Drop) Commands in SQL Server 2005 tip, you have seen how you can track DDL activities in your server instance. But how do we store the events captured by these DDL triggers and store it in a table for reporting purposes?

Solution

In SQL Server 2005, you can get data regarding the event that initiated the DDL trigger by accessing the EventData() function. This function returns information about server or database events and is stored in a variable that uses the XML data type. What we need to do is capture the data returned by the EventData() function and store it in a database table for reporting purposes. You can store that data directly in an XML column or process it first before storing it as ordinary columns in a table. But since the returned type is XML, we need to apply a bit of XQuery against the data generated by the function. To identify the elements in the XML data returned, use Index or Search to locate the topic for the event in SQL Server Books Online. For this particular tip, we will use table-related events. The DDL_TABLE_EVENTS type describes what data can be captured for a CREATE, ALTER or DROP TABLE event.


Let's use the Northwind database for this example. We will create a table that will store the data returned by the EventData() function. We will only choose a few items from the schema generated by the ALTER_TABLE event; namely, the TSQLCommand, PostTime, LoginName, and EventType which is included in every event.

Create the event log table

USE Northwind
GO
CREATE TABLE EvtLog
(
PostTime DATETIME,
LoginName NVARCHAR(100),

EventType NVARCHAR(100),
TSQLCommand NVARCHAR(2000)
)
GO

Create the DDL trigger

CREATE TRIGGER trPreventTblChange
ON DATABASE
FOR ALTER_TABLE
AS
DECLARE @Data XML
SET @Data = EventData()
INSERT EvtLog (PostTime, LoginName, EventType, TSQLCommand)
VALUES
(GETDATE(),
CONVERT(NVARCHAR(100), CURRENT_USER),
@Data.value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(100)'),
@Data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(2000)') ) ;
GO


Code Walthrough

First, we declare a variable named @Data that will be of XML data type

Next, we assign the value returned by the EventData() function to the variable

Then, in the INSERT statement, we retrieve the element values in the @Data variable using XQuery and the value() method. We use the value() method as it takes a second parameter, represented by the value 1 as it is based on the zero index, that is the name of one of the SQL Server built-in data types. The value is returned as an instance of that type. Also, the value() method gives you better results with the formatting of the XML data

Test the trigger

Let's add a new column on the Region table in the Northwind database

USE Northwind
GO

ALTER TABLE Region
ADD newColumn SMALLDATETIME NULL

Querying the EvtLog table will give you the data returned by the EventData() function, stored in tabular format as we have already extracted the element values inside the XML data from inside our trigger definition.

select * from Northwind.dbo.evtlog

Result

postTime LoginName EventType TSQLCommand
2008-08-19 15:37:27.170 dbo Alter_Table ALTER TABLE Region ADD newColumn
SMALLDATETIME null
63
Vote
Add To: del.icio.us Digg Furl Spurl.net StumbleUpon Yahoo


   
Subscribe to this blog 


Just this blog This blog and DailyOrble (recommended)

   

   

   


Comments
1 Comments. [ Add A Comment ]

Comment by TimmyH

September 5th 2008 19:27

Add A Comment

To create a fully formatted comment please click here.


CLICK HERE TO LOGIN | CLICK HERE TO REGISTER

Name or Orble Tag
Home Page (optional)
Comments
Bold Italic Underline Strikethrough Separator Left Center Right Separator Quote Insert Link Insert Email
Notify me of replies
Notify extra people about this comment
Is this a private comment?
List the Email Addresses or Orble Tags of the people you would like to be notified about this comment


One per line max of 30

List the Email Addresses or Orble Tags of the people you would like to be notified about this private comment thread. Only the people in this list will be able to see or reply to your comment.


One per line max of 30

Your Name
(for the email going out to the above list, it can be different to your Orble Tag)
Your Email Address
(optional)
(required for reply notification)
Submit
More Posts
6 Posts
11 Posts
1 Posts
79 Posts dating from May 2008
Email Subscription
Receive e-mail notifications of new posts on this blog:
0

Siddharth sood's Blogs

35 Vote(s)
0 Comment(s)
1 Post(s)
75 Vote(s)
0 Comment(s)
2 Post(s)
35 Vote(s)
0 Comment(s)
1 Post(s)
48 Vote(s)
0 Comment(s)
1 Post(s)
27 Vote(s)
0 Comment(s)
1 Post(s)
67 Vote(s)
0 Comment(s)
2 Post(s)
146 Vote(s)
0 Comment(s)
4 Post(s)
193 Vote(s)
2 Comment(s)
5 Post(s)
24 Vote(s)
0 Comment(s)
1 Post(s)
23 Vote(s)
0 Comment(s)
1 Post(s)
Moderated by Siddharth sood
Copyright © 2006 2007 2008 On Topic Media PTY LTD. All Rights Reserved. Design by Vimu.com.
On Topic Media ZPages: Sydney |  Melbourne |  Brisbane |  London |  Birmingham |  Leeds     [ Advertise ] [ Contact Us ] [ Privacy Policy ]