Suggestions for implementing audit tables in SQL Server?

≯℡__Kan透↙ 提交于 2019-11-27 07:48:01

How much writing vs. reading of this table(s) do you expect?

I've used a single audit table, with columns for Table, Column, OldValue, NewValue, User, and ChangeDateTime - generic enough to work with any other changes in the DB, and while a LOT of data got written to that table, reports on that data were sparse enough that they could be run at low-use periods of the day.

Added: If the amount of data vs. reporting is a concern, the audit table could be replicated to a read-only database server, allowing you to run reports whenever necessary without bogging down the master server from doing their work.

We are using two table design for this.

One table is holding data about transaction (database, table name, schema, column, application that triggered transaction, host name for login that started transaction, date, number of affected rows and couple more).

Second table is only used to store data changes so that we can undo changes if needed and report on old/new values.

Another option is to use a third party tool for this such as ApexSQL Audit or Change Data Capture feature in SQL Server.

HadleyHope

I have found these two links useful:

Using CLR and single audit table.
Creating a generic audit trigger with SQL 2005 CLR

Using triggers and separate audit table for each table being audited.
How do I audit changes to SQL Server data?

Are there any built-in audit packages? Oracle has a nice package, which will even send audit changes off to a separate server outside the access of any bad guy who is modifying the SQL.

Their example is awesome... it shows how to alert on anybody modifying the audit tables.

OmniAudit might be a good solution for you need. I've never used it before because I'm quite happy writing my own audit routines, but it sounds good.

Chris Miller

I use the approach described by Greg in his answer and populate the audit table with a stored procedure called from the table triggers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!