Reputation: 7340
I am very confused about how SQL Server 2005 supports importing and exporting of XML files. I was thinking that BCP was the way to go. However after reading all of the documentation it seems like there is no way to just take a regular table and export it in XML format. The reason that I am asking is that I am working with a client that will be sending me data updates in XML format. We both have the same database with identical structures. He has sent me example XML files. I was assuming that there would be some very simple way to import the data files but I can't figure it out. Can anyone help me understand how to take an XML file and import the data in to an existing table? It should be simple because the XML file was generated from a table with the exact same structure.
Thanks, Corey
Upvotes: 2
Views: 10302
Reputation: 7078
SQL Server 2005 allows to store XML data in two ways: - As a Rowset & - An XML Column
Storing XML Data in a Rowset
Shredding XML document involves the following tasks:
sp_xml_preparedocument
stored procedure to parse the xml document.
This stored procedure reads the xml
document and parses it with the MSXML
parser. The parsed document is an
internal tree representation of
various nodes in the xml doc such as
elements,attributes,text and
comments.openxml(idoc int[in],rowpattern
nvarchar[in],[flags byte[in]]) [WITH
(SchemaDeclaration | TableName)]
sp_xml_removedocument
stored procedure.For example, following is the data available in an XML doc:
DECLARE @Doc int
DECLARE @XMlDoc nvarchar(1000)
SET @XMLDoc = N'<ROOT>
<Customer CustomerID="JHO1" Name="Jack">
<Order OrderID="1001" CustomerID="JH01"
OrderDate="2009-07-04T00:00:00">
<OrderDetail ProductID="11" Quantity="12"/>
<OrderDetail ProductID="22" Quantity="10"/>
</Order>
</Customer>
<Customer CustomerID="SG01" Name="Steve">
<Order OrderID="1002" CustomerID="SG01"
OrderDate="2009-08-16T00:00:00">
<OrderDetail ProductID="32" Quantity="3"/>
</Order>
</Customer>
</ROOT>'
To view this xml data in a rowset, you need to execute the following statements:
1. Create internal representation of xml document EXEC sp_xml_preparedocument @Doc OUTPUT, @XMLDoc
2. Execute the following query to store the data in a table using OPENXML function:
INSERT INTO CustomerDetails
SELECT *
FROM openxml (@Doc, '/ROOT/Customer' , 1)
WITH (CustomerID varchar(10),
Name varchar(20) )
The data will be displayed as in the table:
CustomerID | Name |
___________|_________|
JH01 | Jack |
| |
SG01 | Steve |
___________|_________|
3. Remove the internal tree from the memory by executing
EXEC sp_xml_removedocument @Doc
You're done now.
I think this method would help rather than the other one, ie, storing xml data as XML Column.
Upvotes: 1
Reputation: 56
Why on earth use XML as exchange format in the first place?
If you have the exact same data model on both sides plain text files or BCP files will perfectly fit your needs and also be way smaller in size (ie extract and load faster)!
Just because XML is "state of the art" it dosn't meen it is always the best choice! Of course if you belive that you some time in the future need to exchange data with 3rd parties not sharing the same data model or even the same platform or interface the case is of course different.
Upvotes: 2
Reputation: 1912
I assume the client is using FOR XML to wrap the relational data as an xml document? There is no automagic way to shred the xml back to the original relational structure, but you should be able to do it programatically if the original xml generation also uses the XMLSCHEMA directive. If the client includes this, then an xml schema will be generated that describes which column (and its type) is represented where in the xml instance. I started playing with this approach awhile back but never finished it.
Upvotes: 0
Reputation: 432311
You have to parse the XML as into a table like format. Remember: xml is just text so you have to tell SQL Server what datatypes are etc.
There is no one shot magic bullet INSERT MyTable FROM File = 'bob.xml'
Upvotes: 0