Adu
Adu

Reputation: 381

TimeStamp in sql

I have a table named Table1 which contains an ID and TimeStamp.

Table structure

ID  TimeStamp 
1   0x0000000000047509 

But when I compare the combination of these fields, it always shows false. What is the reason for this?

My Query is :

 DECLARE @ID int 
 DECLARE  @TimeStamp  timestamp 

 SET @ID = 1 
 SET @TimeStamp = 0x0000000000047509 

 If EXISTS(SELECT 1 FROM Table1 WHERE ID = @ID AND TimeStamP = @TimeStamp) 

 BEGIN 
       SELECT 1 AS RetVal 
 END 

 ELSE 

 BEGIN 
       SELECT -1 AS RetVal     
 END 

This will simply work in a Query analyzer.But this will not work in a stored Procedure. Any solution for this?

Upvotes: 1

Views: 6317

Answers (2)

Iain
Iain

Reputation: 11244

It should work, I have tried it myself and it worked fine. I ran the following from an SP and it output OK.

DECLARE @t timestamp
SET @t = 0x00000000000055F5

IF EXISTS(SELECT 1 FROM dbo.[User] WHERE [Timestamp]=@t AND UserId=10)
BEGIN
    PRINT 'OK'
END

Maybe your timestamp has changed before the SP runs. The timestamp changes every time you insert or update the row. Really rowversion is a better name for it IMHO.

Good resrouces: timestamp and Timestamps vs Datetime data types

Upvotes: 1

James
James

Reputation: 1691

If I recall, TimeStamp in SQL server is not actually a time stamp but is intended for mostly internal use (I used to use TimeStamps for last updated type fields in MySQL but thats not the right type in SQL Server it seems). I believe you will just want to use a datetime or datetime2 data type depending on the accuracy you want.

Here is a link to the choices available: http://msdn.microsoft.com/en-us/library/ms186724.aspx

Upvotes: 2

Related Questions