Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Server Audit

Author  Topic 

josephj1989
Starting Member

7 Posts

Posted - 2010-05-15 : 23:59:48
Hi
I am relatively new to SQL Server and I am designing a small database .Some tables need to store information like date time of last update and user who last updated each record.This is a typical audit requirement.I can add extra fields to each table for this.
But I am wondering whether I can use any standard SQL Server Audit/Change Tracking features to satisfy my requirement.

Please advice

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-16 : 03:51:52
Is your application using application security or SQL security?

You can use a trigger to manage the last update column.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

josephj1989
Starting Member

7 Posts

Posted - 2010-05-16 : 04:34:03
Hi Tara
thanks for your reply. Mine is a simple ASP.net c# web application using Linq for data access.I can easily populate the said audit fields from c#(in the linq entities).But I wanted to avoid doing all that if some automatic auditing option is available in sql server (In this case I dont even need to create audit fields in database tables). So I just wanted to confirm that no automatic features are available before I (manually) implement these.
thanks
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-05-16 : 05:22:29
Implement them using a Database Trigger, then it doesn't matter how the data is updated, the UpdateDate column will always be set.

All our tables (well, 99.99% of them!) have Create/Update Date and Create/Update User. We have an additional field for "Edit Version" to prevent someone overwriting a modification by someone else. Probably using a ROWVERSION datatype column would be better. (Retrieve the RowVersion value, along with the other values your application needs, then when the application saves the data it includes that in the WHERE clause

UPDATE U
SET Col1 = 'Value1', ...
FROM MyTable AS U
WHERE U.MyID = '123'
AND U.MyRowVersion = '456'
IF @@ROWCOUNT <> 1 THEN ... update failed, raise error! ...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-16 : 07:37:06
we also have similar columns called (datecreated,createdby,datemodified,modifiedby,timestampvalue) in all tables for audit. the date fields will be defaulted to GETDATE() and created/modifiedby to SUSER_SNAME(). In addition, we will have a trigger for update to set datemodified and modifiedby. the timestampvalue is rowversion column which will be retrived before update and then checked before doing save

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-16 : 12:58:02
If your application is using application security (users are rows in a table rather than distinct SQL logins), then SUSER_SNAME() will not work as you'll only get the SQL login the application is using. The application would have to send the userid in the DML transaction if you are using application security.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -