views:

2702

answers:

7

I need a select from table which does not have column that tells when row was inserted, only timestamp column (values like: 0x0000000000530278). Some data was imported to the table yesterday and now I need to find out what exactly was imported :(

Is there a way to do it using only timestamp info? Here I found that:

  • Timestamp is a 8 bytes sequential Hex number, that has nothing to do with neither the date nor the time.
  • To get the current value of timestamp, use: @@DBTS.

perhaps there is a way to find what was timestamp value around specific time? That would help to form a select. Or maybe there is a well known solution?

+3  A: 

The timestamp datatype in SQL Server 2005 is a synonym of rowversion and is just a number that is automatically incremented with each row update.

You can cast it to bigint to see its value.

To get what you want for new or updated rows, you should propably add another datetime column (lastupdate) and a trigger to update that column with each update.

For rows that have already been inserted in the past I don't think that you can do something to find the exact time.

Panagiotis Korros
That explains timestamp column type, but does not answer my question :|
Dandikas
The only solution I see is to add another datetime column and update that colunn with a triger. I edited my answer to add this.
Panagiotis Korros
Yes, that would solve the problem for future cases, but mine is in past, so adding trigger can't help.
Dandikas
Too bad then, because I think that nothing can be done for these rows.
Panagiotis Korros
A: 

I think your best bet is to restore a backup from before the inserts and compare the backuped table with the current table.

Jonas Lincoln
+2  A: 

I'm afraid it's not possible to convert/cast a TIMESTAMP to a DATETIME. They have entirely different uses and implementations that are incompatible.

See this link http://www.sqlteam.com/article/timestamps-vs-datetime-data-types

Books on-line also says http://msdn.microsoft.com/en-us/library/aa260631.aspx

The SQL Server timestamp data type has nothing to do with times or dates. SQL Server timestamps are binary numbers that indicate the relative sequence in which data modifications took place in a database. The timestamp data type was originally implemented to support the SQL Server recovery algorithms.

evilhomer
Perhaps there is a way to find what was timestamp value around specific time? Import is a rear thing, so time gap is significant (precision is not needed)
Dandikas
+7  A: 

The Transact-SQL timestamp data type is a binary data type with no time-related values.

So to answer your question: Is there a way to get DateTime value from timestamp type column?

The answer is: No

kristof
This answers the question, unfortunately does not help to solve the problem. (Your second answer is an idea I'll try)
Dandikas
+3  A: 

Another answer to you question:

If the timestamp column is the only resource for you recovery (no backups etc) you may try to use the following logic

Timestamp is simply a value of a counter that is incremented for each insert or update operation that is performed on a table that contains a timestamp column.

If the data import that happened yesterday was one insert of several records you may see a sequence of numbers in the timestamp column like e.g:

0x00000000000007D1
0x00000000000007D2
0x00000000000007D3
0x00000000000007D4
0x00000000000007D5

The most recent sequence can be your added data (of course it is not guarantied) You con combine that knowledge with other things (like auto-increment column if you use them) to identify the records you are interested in.

kristof
That's close to what I need ... will give it a try.
Dandikas
A: 

To identify new rows by timestamp you need to keep track of the timestamps that were there beforehand. In a pinch you could:

  • Restore a previous version somewhere else.
  • Copy the data from both tables into a scratch database.
  • Identify the inserted data from the timestamps present in one but not the other.

With a minor risk of false positives if anything else has been going on in the DB this will get you a reasonably good difference.

For a more robust check you could calculate MD5 or SHA-1 hashes with Hashbytes on the row contents to give you a difference with a very low probability of collision (see this wikipedia article on Birthday attacks for a discussion of this problem).

ConcernedOfTunbridgeWells
A: 

Could you use a Transaction Log reader tool to determine which data was imported?

Mitch Wheat