REATE TRIGGER tr_Security ON ALL SERVER For DDL_LOGIN_EVENTS AS BEGIN INSERT INTO DDLTriggerTest..EventtableData (XMLEvent, DatabaseName, SystemUser) VALUES (EVENDATA(), DB_NAME(), SYSTEM_USER) ENDThis script creates the trigger that declares that we want to look for all DDL Login events that occur on the server. The information resulting when this trigger fires will go into the DDLTriggerTest table we created earlier. Notice that we use three functions to insert data into our table. In this context, the EVENTDATA function will gather information about logins because that's the event specified in the statement FOR DDL_LOGIN_EVENTS. The SYSTEM_USER function returns the login executing the current statement, and, as you may guess, the DB_NAME() function returns the name of the database in which the current statement is being executed. Because creating a login is a server level event, our audit trail records that the statement occurred in the master database.
To test our new trigger, we have to raise a LOGIN event on the server to be caught. The easiest way to do this is to create a new login by issuing a command such as CREATE LOGIN TestLogin WITH PASSWORD = '123456xxYYbaz.'
After running this statement, if we look in the Messages window, we see the message (1 rows(s) affected). This statement occurs because of the auditing that we have put in place. When we ran the CREATE LOGIN statement on the server, the DDL trigger was fired and a record was inserted into our EventTableData table in the DDLTriggerTest database.
What's in your XML?
To prove that the record was written to our table, you can run a SELECT query on the DDLTriggerTest table. If you're used to working with SQL Server 2000 then the data in the XMLEvent column may look strange to you. In SQL Server Management Studio (SSMS), you click on the link in the XMLEvent column to view the XML data it contains.
The XMLEvent field is populated by the EVENTDATA() function from our INSERT statement in our trigger. In this case, the EVENTDATA() function returns XML data related to the login, including the date and time of the login, the server anme, the user ID, and the machine system ID.
Only the beginning
The DDL_LOGIN_EVENT trigger is just one of over five dozen DDL events you can monitor that can help document the security of your system. Other DDL events of interest for Sarbanes-Oxley audits include creating or dropping other database objects, such as table, procedures, triggers, views, functions, etc.
In this article, I limited our discussion to server-level events. In a future article, I'll demonstrate a DDL trigger that tracks a database-level event. I'll also show you how those triggers can do more than just track events--they can actually prevent unauthorized events from occurring in your database environment that you don't want to occur.
Tim Chapman is a Database Administrator for a large company in the financial services industry. Post a comment below or write to Tim at chapman.tim@gmail.com.



1
Peter Loo - 11/05/06
This is a handy tool. Can it be customised to track changes to database tables (instead of just sql events)?
If changes are inserted into the audit trail table as snapshot XML data, can we use sql to report on differences between 2 snapshot records?
» Report offensive content
2
Peter Joint - 12/05/06
Neat approach, although I'd be concerned about the performance impact of putting triggers on data changes. Another approach is what Embarcadero have done with their DSAuditor solution (aquired from Ambeo last year) - having a solution that tracks all that, doesn't involve changing the database and is zero impact, I see as another good way to approach this issue.
» Report offensive content
3
Tim Chapman - 26/05/06
You can use DML triggers to track changes to data.
These triggers make use of the INSERTED and DELETED tables. In 2005 you can also use the OUTPUT statement for custom DML statement auditing. I've written an article about the OUTPUT statement: http://techrepublic.com.com/5100-9592_11-6074046.html?tag=search#
» Report offensive content
4
Tony Finnemore - 24/07/06
The major problem I perceive with in-house developed solutions, even smart ones like these, is that "authorised users" can still conduct "unauthorised activities" if they can gain access to the respository of audit data or the triggers etc. Then there is the performance impact of triggers - especially on high transaction systems!
There is only one SQL Server auditing product I know of which is self auditing and can tell you when and who changed the auditing configurations or audited data. This auditing tool - SQL compliance manager - from Idera was developed with the assistance of Ernst & Young, DBAs from the finance world and Microsoft who provided assistance with minimising impact on SQL Server resources.
» Report offensive content