Avatar of batchakamal
batchakamal
Flag 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

Microsoft SQL Server 2005XML

Avatar of undefined
Last Comment
Guy Hengel [angelIII / a3]

8/22/2022 - Mon
ee_rlee

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

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.
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.


Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Guy Hengel [angelIII / a3]

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.