Link to home
Start Free TrialLog in
Avatar of batchakamal
batchakamalFlag for India

asked on

Comparing TIMESTAMP column in SQL Server 2005

Hi,

We are receiving a XML document as input to update the records in a table. Inorder to update the data we are using the Primary Key columns (Identity Column) and a timestamp column.

As part of the process, we are loading the XML data into a temporary table. When we load we are loading the timestamp column into a varchar column to get the same timestamp value, otherwise if we load into timestamp data type column it is inserting some new value.

Now when we update the records using the update statement it is throwing an error message for implicit conversion. We have tried convert functions and cast function to convert the data while updating, but it is not working.

To simulate our problem, just run the attached script.

Advice accordingly.
--To create the table
 
CREATE TABLE [dbo].[EMPLOYEE](
	[OBJECT_ID] [int] IDENTITY(1,1) NOT NULL,
	[EMPLOYEENAME] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[EMPLOYEESALARY] [int] NULL,
	[DEPTID] [int] NULL,
	[TMSTAMP] [timestamp] 
) ON [PRIMARY]
 
GO
 
 
--Inserting two records to update later
 
insert into EMPLOYEE(EMPLOYEENAME)
values('ABC')
 
insert into EMPLOYEE(EMPLOYEENAME)
values('XYZ')
 
--SP Content
 
DECLARE @XMLDocPointer INT,
@strXML xml,
@sql VARCHAR(8000)
---CODE TO GET THE NEWLY INSERTED VALUE
SET @strXML ='
<RECORD>
   <EMPLOYEE>
        <OBJECT_ID>1</OBJECT_ID>
        <EMPLOYEENAME>AAAA</EMPLOYEENAME>
		<TMSTAMP>0x0000000000000FA7</TMSTAMP>
	</EMPLOYEE>
	<EMPLOYEE>
		<OBJECT_ID>2</OBJECT_ID>
        <EMPLOYEENAME>ZZZZ</EMPLOYEENAME>
		<TMSTAMP>0x0000000000000FA8</TMSTAMP>
       
  </EMPLOYEE>
</RECORD>
'
EXEC sp_xml_preparedocument @XMLDocPointer OUTPUT, @strXML
 
SELECT * into #T_TEMP
FROM
OPENXML(@XMLDocPointer,'/RECORD/EMPLOYEE',2)
WITH (OBJECT_ID INT,EMPLOYEENAME VARCHAR(30), EMPLOYEESALARY INT,DEPTID INT,TMSTAMP varchar(50))
 
SELECT * FROM #T_TEMP
 
---ERROR IN THE BELOW UPDATE STATMENT WHEN COMPAREING THE TIMESTMAP(IN WHERE CONDITION)
UPDATE EMPLOYEE
SET EMPLOYEE.EMPLOYEENAME=B.EMPLOYEENAME
FROM EMPLOYEE, #T_TEMP B
WHERE EMPLOYEE.OBJECT_ID=B.OBJECT_ID AND EMPLOYEE.TMSTAMP=B.TMSTAMP
 
select * from EMPLOYEE
 
--SELECT convert(varbinary(8),TMSTAMP) as t FROM EMPLOYEE
 
drop table #T_TEMP

Open in new window

Avatar of ee_rlee
ee_rlee
Flag of Philippines image

hi, I tried to cast B.TMSTAMP to timestamp and it works..
UPDATE EMPLOYEES
SET EMPLOYEES.EMPLOYEESNAME=B.EMPLOYEESNAME
FROM EMPLOYEES, #T_TEMP B
WHERE EMPLOYEES.OBJECT_ID=B.OBJECT_ID AND EMPLOYEES.TMSTAMP=cast(B.TMSTAMP as timestamp)

Open in new window

Avatar of Aneesh
Hello batchakamal,

method 1 :  

WHERE EMPLOYEE.OBJECT_ID=B.OBJECT_ID AND EMPLOYEE.TMSTAMP=CAST (B.TMSTAMP AS timestamp)


Meht 2:

SELECT * into #T_TEMP
FROM
OPENXML(@XMLDocPointer,'/RECORD/EMPLOYEE',2)
WITH (OBJECT_ID INT,EMPLOYEENAME VARCHAR(30), EMPLOYEESALARY INT,DEPTID INT,TMSTAMP TimeStamp)
 
SELECT * FROM #T_TEMP
 
---ERROR IN THE BELOW UPDATE STATMENT WHEN COMPAREING THE TIMESTMAP(IN WHERE CONDITION)
UPDATE EMPLOYEE
SET EMPLOYEE.EMPLOYEENAME=B.EMPLOYEENAME
FROM EMPLOYEE, #T_TEMP B
WHERE EMPLOYEE.OBJECT_ID=B.OBJECT_ID AND EMPLOYEE.TMSTAMP=B.TMSTAMP


GOD Bless,
Aneesh R.
Avatar of batchakamal

ASKER

Hi,

I have tried all the above methods, it is not giving any error message but it is not updating the corresponding records.

Note:- Also after you insert the values, copy the timestamp value and replace it in the XML document.


ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial