SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Automate Audit Trigger Generation

By walkerjet,

I have been tasked with a daunting job of scripting I/U/D triggers for our new database. The job of the triggers is to Audit the activity on the tables. What column was changed to what value on what table and by whom. With over 60 tables, this is a daunting task. So off I went scouring this forum for functions and ideas. Finally, I got it together and it works. We have 1 Audit table for reporting and the rest is in the soup of stored procedures and functions.

The only caveat to this is that each table that you are going to audit has to have a ModifiedById (unique identifier) and that the triggers will not work for text or ntext columns.

The single point of entry is to execute the vp_GenerateTriggers.

Parameters are Table Name and Action. Table name can be any table or 'ALL' for all tables. Action can be I, U, D - you guessed it and A for all.

So execute vp_GenerateTriggers 'ALL', 'A' will generate insert update and delete triggers for all tables in the database.

Gut the cript, modify it to your needs, or change the templates in the stored procedures to meet your needs.


Total article views: 985 | Views in the last 30 days: 4
Related Articles


Triggers for audit


Auditing Through Triggers

In this article by Robert Marda, he shows you how to setup a simple auditing system through triggers...


Triggers for Auditing

Have you ever needed to audit your SQL Server 2000 database for changes to a table? A simple auditin...


Script Generates Audit Triggers

This script generates a audit trigger for each table in the database. The trigger then logs when eac...