troubleshooting Question

Comparing TIMESTAMP column in SQL Server 2005

Avatar of batchakamal
batchakamalFlag for India asked on
XMLMicrosoft SQL Server 2005
4 Comments1 Solution1232 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros