?
Solved

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

Posted on 2011-09-08
3
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Assisted Solution

by:mimran18
mimran18 earned 800 total points
ID: 36501606
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 1200 total points
ID: 36501783
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:Richard Quadling
ID: 36501941
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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

752 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