Solved

Want date/time of every change to row in SQL Server table

Posted on 2011-09-08
3
275 Views
Last Modified: 2012-05-12
I am using SQL Server Express 2008 and have a table where I want to know the date/time when the rows in the table are modified/added.  Do I need to use a datetime field with a trigger to do this or is there a better way?  Either way I would like an example to follow.
0
Comment
Question by:canuckconsulting
  • 2
3 Comments
 
LVL 9

Assisted Solution

by:mimran18
mimran18 earned 200 total points
Comment Utility
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 300 total points
Comment Utility
If the intent is to have a physical datetime to compare against, then a datetime2 column is required and using an insert/update trigger is the way to go (http://msdn.microsoft.com/en-us/library/bb677335.aspx).

Alternatively, this sort of question can often mean "I want to know what has changed since the last time I looked". In this case the rowversion type may be better suited (http://msdn.microsoft.com/en-us/library/ms182776.aspx).

This is a completely automatic column. No trigger needed.

All you need to record is the last rowversion. At the start of the process in which you want to find the changes, you can use the @@DBTS variable (http://msdn.microsoft.com/en-us/library/ms187366.aspx) to find what the current rowversion number is and compare that with the previously stored rowversion.

Now, you can find what rows have changed between the two rowversion numbers.

If you put your work in a transaction, then you will get stale data if a row is altered whilst you are looking at the rows that have been previously tagged.

In this case it is up to you to decide to in a transaction or not.

It depends upon what you intend to do with the data.
0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
Please also read http://msdn.microsoft.com/en-us/library/bb839514.aspx regarding MIN_ACTIVE_ROWVERSION, which explains the issues with using rowversion data in transactions and blocks of changes.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now