Discussion Locked

Audit trail of tables on MS SQL Server

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
i need some suggestions on how i can create an audit trail of the tables and all the fields in it on every add,update and delete action. a sample code will be highly appreciated.

1 reply

by imarek3

This is what we came up with. We determined several ways to implement an audit table which requires the table you want to audit, the audit table with no keys or indexes (used to hold daily inserts, deletes and updates) and an audit report table (used to inquiries on) which can be indexed for reporting and inquiry purposes. You don't want to run inquiries against the audit table itself, because it could impact response times since it is updated at the same time as the table being audited.

Weuse triggers to update our tables, these triggers are responsible for saving audit information to the audit table. The audit table is to be dumped every night to the audit report table, where inquiries and reports can be run from. We came up with 2 different ways to implement this.

1) Write an initial row to the audit table report table for each row in the table being audited. Inserts and deletes would be written to the table and the audit table. On an update, it would be written to thetable and an after image would be written to the audit table. This will give you what the row initially looked like and any changes. The problem with this approach is that if you archive your audit report table, you may not be able to easily see what the row looked like before the change unless you go back to the archive.

2) The other method would be not to put an initial row out to the audit report table. In this approach, the inserts and deletes would be written to the table and the audit table as before, but the updates would be handled differently. On an update to the table, a before and after row would be written to the audit table. In this approach the audit report table starts out smaller, but could grow faster depending on the number of updates being made. You don't have the problem of seeing what the row looked like before the update if an archive is done.

This discussion is closed to new replies.