Better way to handle 'auditing' columns such as created_by, created_dt, last_updated_by, last_updated_dt

Jim Horn
Jim Horn used Ask the Experts™
on
Hi guys

In a previous question I made the below comment, which resulted in feedback that there are better ways to handle these values, to include splitting them into a separate table.

As a general rule any table I design that has data entry, I'll add four 'auditing' columns to it that populate via INSERT and UPDATE triggers:
  created_dt  -  datetime - of the entry
  created_by_id -  varchar(30) - SUSER_NAME() of the person creating it
  last_updated_dt  -  datetime - of any insert-update
  last_updated_by_id - varchar(30) - SUSER_NAME() of the person updating it.

Points for advice on a better way to handle this.

Thanks.
Jim
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AneeshDatabase Consultant
Top Expert 2009

Commented:
Have you tried CDC feature yet ?
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Author

Commented:
No.  Have you implemented CDC, and if so how did you do it, and what were the benefits?
AneeshDatabase Consultant
Top Expert 2009
Commented:
I have implemented on  couple of database servers to catch the data capture,  this is inbuilt feature of sql server, which captured the data changes on TL , so it will be faster compared to triggers.  In your case I realized that, you need to capture the User who made the changes too, which I am afraid, cant be done thru CDC;  every thing related to the changed data can be audited thru CDC.
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
Change Tracking is more lightweight and can tell you when a change occurred for a given row ... but not what was changed, which CDC can do.  CT can't directly tell you "who" either.  

Default constraints could provide datetime and SUSER_SNAME() for created.
MSSQL Senior Engineer
Distinguished Expert 2017
Commented:
In a separate table you can add more features as for example if the operation was an Insert, Delete or Update, so you can also track deletes.
At the same table you can't track deletes and you don't have an history, only the last change.
Also, you can put the separate table in another file, filegroup or even database. I think it's a more flexible option but it always depends on the goal of the solution that you want.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial