Solved

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

Posted on 2011-09-08
3
283 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 200 total points
ID: 36501606
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 300 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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