views:

53

answers:

1

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

My stored procedure is as follows

 set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON go

CREATE PROCEDURE [dbo].[Check] ( @XMLDoc ntext )AS

BEGIN SET NOCOUNT ON SET XACT_ABORT ON

DECLARE @ID bigint DECLARE @TimeStamp timestamp DECLARE @hDoc int

EXEC sp_xml_PrepareDocument @hDoc OUT, @XMLDoc

SELECT @ID = ID ,@TimeStamp = [TimeStamp] FROM OPENXML (@hdoc,'/XML') WITH ( ID bigint 'ID' ,[TimeStamp] timestamp 'TStamp')

IF @@ERROR<>0 BEGIN EXEC sp_xml_RemoveDocument @hDoc SELECT -620 AS RetVal RETURN
END

IF NOT EXISTS(SELECT 1 FROM Table1 WHERE ID= @ID AND Timestamp = @TimeStamp ) BEGIN SELECT -1 AS RetVal END ELSE BEGIN SELECT 1 AS RetVal END

END

+1  A: 

That's odd, the query works fine for me in SQL Server 2005, compatibility mode 80.

The only thing that jumps out to me is that Timestamp is a reserved word, so to be on the safe side you might want to add brackets around Timestamp to escape it as follows:

If EXISTS(SELECT 1 FROM Table1 WHERE [ID] = @ID AND [TimeStamP] = @TimeStamp) ...
LittleBobbyTables
it is not working for me in Sql 2000.Have u checked this in 2000?
Adu
@Adu - yes, this worked for me in SQL 2000 as well. Are the datatypes for ID and Timestamp `int` and `timestamp` respectively?
LittleBobbyTables
the datatype for ID is bigint
Adu
Is this a query done through Query Analyzer or through a stored procedure?
LittleBobbyTables
i have used it in a storedProcedure
Adu
Can you edit your question and show how you are calling the procedure? Are you passing the timestamp in as a varchar by chance?
LittleBobbyTables
no i am passing it as timestamp
Adu
Yeah, that would have been helpful to have up front... I'll take a look at this and get back to you.
LittleBobbyTables