Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-08
3
Medium Priority
?
288 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 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