Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 OPENXML Insert/Update

Author  Topic 

John Sourcer
Yak Posting Veteran

91 Posts

Posted - 2008-03-11 : 03:42:24
Hi All,

The code (http://sqlxml.org/faqs.aspx?faq=100) below mirrors mine but is simplfied:

declare @i int

exec sp_xml_preparedocument @i output,
'<mydata>
<test xmlID="3" xmlData="blah blah blah"/>
<test xmlID="1" xmlData="blah"/>
</mydata>'

insert into test
select xmlID, xmlData
from OpenXml(@i, 'mydata/test')
with (xmlID int, xmlData nvarchar(30))
where xmlID not in (select xmlID from test)

update test
set test.xmlData = ox.xmlData
from OpenXml(@i, 'mydata/test')
with (xmlID int, xmlData nvarchar(30)) ox
where test.xmlID = ox.xmlID

exec sp_xml_removedocument @i


My question is when running this stored procedure for the first time, isn't one inserting AND then updating each row?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-03-11 : 07:08:26
Not exactly. Its checking for whether records with same xmlID exists in test table and inserting one if not exists or updating it if it already exists.
Go to Top of Page
   

- Advertisement -