<GBez@discussions.microsoft..com> wrote in message news:c959c905-bfac-4471-b7a1-059439279ef9@discussions.microsoft.com...Hi, I have a string of xml stored in a ntext field of a table in a SQL Server 2000 database. What I have to do is strip out the data stored in the string and store it in a table in the database. I have succeeded so far in using the OpenXML SQL command but that puts it in a edge table where I am struggling to extract the data as the fields that I require are stored as rows in the edge table and I dont know how to pivot the data so as to select them as fields. Whats more, each XML string can contain more than one record of data to insert which complicates things further when trying to identify which row of data in the edge table is relevant to which record I need to insert.
Can anyone help me with an alternative method of importing this data
Thanks in advance
Import XML string into DB
I'm not sure what an "edge" table is.
Can you post a sample of the XML, the OPENXML
syntax you're currently using, and the output you're trying to
achieve

Import XML string into DB
Rodrigo Wolburg
--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
Mike Harsh
Hi, I have a string of xml stored in a ntext field of a table in a SQL Server 2000 database. What I have to do is strip out the data stored in the string and store it in a table in the database. I have succeeded so far in using the OpenXML SQL command but that puts it in a edge table where I am struggling to extract the data as the fields that I require are stored as rows in the edge table and I dont know how to pivot the data so as to select them as fields. Whats more, each XML string can contain more than one record of data to insert which complicates things further when trying to identify which row of data in the edge table is relevant to which record I need to insert.
Can anyone help me with an alternative method of importing this data
Thanks in advance
TTE
--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
JosRyan
--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
Jasimons
Michael Pollard
try the following XML string:
<Update xmlns="http://website.com/">
<inputDs>
<xs:schema id="1q2e" xmlns="" xmlns:xs="http://website" xmlns:msdata="value">
<xs:element name="12312" msdata:IsDataSet="true" msdata:Locale="en-ZA">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="tb_InUpdate">
<xs:complexType>
<xs:sequence>
<xs:element name="memberid" type="xs:int" minOccurs="0" />
<xs:element name="firmid" type="xs:int" minOccurs="0" />
<xs:element name="recordid" type="xs:int" minOccurs="0" />
<xs:element name="Amount" type="xs:decimal" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<MDataset xmlns="">
<tb_InUpdate diffgr:id="tb_InUpdate" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<memberid>111111</memberid>
<firmid>22222</firmid>
<recordid>33333</recordid>
<Amount>300</Amount>
</tb_InUpdate>
<tb_InUpdate diffgr:id="tb_InUpdate" msdata:rowOrder="1" diffgr:hasChanges="inserted">
<memberid>123123</memberid>
<firmid>234</firmid>
<recordid>2342</recordid>
<Amount>400</Amount>
</tb_InUpdate>
</MDataset>
</diffgr:diffgram>
</inputDs>
</Update>
This is more like what I will be importing where I will insert the values of memberid, firmid, recordid and amount for each record.
Any way to tweak the code to import this string, or possible an alternative method
Omar Hector 123
Hi guys, sorry I havent responded to your questions, been out of the office. I am currently using the following code to import the XML string into the edge table:
declare @idoc int
declare @doc varchar(8000)
set @doc = '< xml version="1.0" >
<Books>
<Book>
<Title>XML Application Development with MSXML 4.0</Title>
<Publisher>Wrox Press</Publisher>
<DateOfPurchase>2/1/2002</DateOfPurchase>
</Book>
<Book>
<Title>Professional SQL Server 2000 XML</Title>
<Publisher>Wrox Press</Publisher>
<DateOfPurchase>9/10/2001</DateOfPurchase>
</Book>
</Books>'
--Create an internal representation of the XML document.
exec sp_xml_preparedocument @idoc OUTPUT, @doc
-- SELECT statement using OPENXML rowset provider
SELECT * into #xml
FROM OPENXML (@idoc, '/')
EXEC sp_xml_removedocument @idoc
select * from #xml
What I need to get now is the data from the xml string i.e. the title, publisher and date of purchase and insert it into a table. Notice there 2 records in this particualr string. Make sense to you guys
Chango V.
Ed Psyk
fuzzfoot
rumen101010
declare @doc varchar(8000)
set @doc = '< xml version="1.0" >
<Books>
<Book>
<Title>XML Application Development with MSXML 4.0</Title>
<Publisher>Wrox Press</Publisher>
<DateOfPurchase>2/1/2002</DateOfPurchase>
</Book>
<Book>
<Title>Professional SQL Server 2000 XML</Title>
<Publisher>Wrox Press</Publisher>
<DateOfPurchase>9/10/2001</DateOfPurchase>
</Book>
</Books>'
--Create an internal representation of the XML document.
exec sp_xml_preparedocument @idoc OUTPUT, @doc
-- SELECT statement using OPENXML rowset provider
SELECT *
FROM OPENXML (@idoc, '/Books/*')
WITH
(
Title VARCHAR(250) 'Title',
Publisher VARCHAR(100) 'Publisher',
DateOfPurchase SMALLDATETIME 'DateOfPurchase'
)
EXEC sp_xml_removedocument @idoc
--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
furtivefelon
--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
David McComb
FROM OPENXML (@idoc, '/Books/*', 2)
WITH
(
Title VARCHAR(250) ,
Publisher VARCHAR(100) ,
DateOfPurchase SMALLDATETIME
)
FROM OPENXML (@idoc, '/Books/*')
WITH
(
Title VARCHAR(250) 'Title',
Publisher VARCHAR(100) 'Publisher',
DateOfPurchase SMALLDATETIME 'DateOfPurchase'
)
FROM OPENXML (@idoc, '/Books/*', 1)
WITH
(
Title VARCHAR(250),
Publisher VARCHAR(100) ,
DateOfPurchase SMALLDATETIME
)
FROM OPENXML (@idoc, '/Books/*')
WITH
(
Title VARCHAR(250) '@Title',
Publisher VARCHAR(100) '@Publisher',
DateOfPurchase SMALLDATETIME '@DateOfPurchase'
)