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 2000 Forums
 Transact-SQL (2000)
 OPENXML with loop??

Author  Topic 

jgn1013
Starting Member

24 Posts

Posted - 2009-05-27 : 14:37:29
If i was to feed this xml string into a stored procedure, how do I go about inserting this data into a table, see example below:


<root>
<userid>12345</userid>
<person>
<fname>John</fname>
<lname>Dow</lname>
<note>some notes here...</note>
</person>
<person>
<fname>Jane</fname>
<lname>Doedet</lname>
<note>notes goes here...</note>
</root>


table A
---------
userid.....fname.....lname.....notes
12345.....John.......Dow........some notes here
12345.....Jane.......Doedet....some notes here


TIA

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-27 : 14:44:20
See examples in Books Online for stored procedure
sp_preparedocument

The trick is to handle the xml content as a "virtual" table.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

jgn1013
Starting Member

24 Posts

Posted - 2009-05-27 : 15:18:04
got it .. thanks



DECLARE @xmlDoc VARCHAR(8000)
DECLARE @xmlHandle INT
BEGIN
SET @xmlDoc =
'
<root>
<userid>12345</userid>
<person>
<fname>John</fname>
<lname>Dow</lname>
<note>some notes here...</note>
</person>
<person>
<fname>Jane</fname>
<lname>Doedet</lname>
<note>notes goes here...</note>
</person>
</root>

'

EXEC sp_xml_preparedocument @xmlHandle OUTPUT, @xmlDoc
Insert Into test_table
SELECT * FROM OPENXML (@xmlHandle, '/root/person', 2) WITH
(
userid VARCHAR(20) '../userid',
fname VARCHAR(50) './fname',
lname VARCHAR(50) './lname',
notes VARCHAR(50) './note'
)


EXEC sp_xml_removedocument @xmlHandle
END



Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-27 : 15:43:50
You're welcome.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -