OPENXML Enhancements in SQL Server 2005
Up until now, we have
been composing XML from rows of data, but what if we already have XML
data and we want to shred it back into relational data? Well, SQL Server
2000 introduced a feature for this purpose called OPENXML. OPENXML is a system function that allows an XML document to be shredded into T-SQL rows. SQL Server 2005 also has the OPENXML function—with some enhancements, of course.
To shred data into relational rows using OPENXML, you must first create an XML document handle using the system stored procedure sp_xml_preparedocument.
This system stored procedure takes an XML document and creates a
representation that is referenced via a handle, which it returns via an OUTPUT parameter. OPENXML uses this handle along with a specified path and behaves like a database view to the XML data, so you simply choose SELECT from the OPENXMLSELECT from a table or a view. The following code shows an example of OPENXML in action. function just as you would
USE AdventureWorks
DECLARE @int int
DECLARE @xmlORDER varchar(1000)
SET @xmlORDER ='
<ROOT>
<Customer CustomerID="BRU" ContactName="Andrew Brust">
<Order CustomerID="BRU" EmployeeID="5" OrderDate="2005-11-04">
<OrderDetail OrderID="10248" ProductID="16" Quantity="12"/>
<OrderDetail OrderID="10248" ProductID="32" Quantity="10"/>
</Order>
</Customer>
<Customer CustomerID="ZAC" ContactName="Bill Zack">
<Order CustomerID="ZAc" EmployeeID="3" OrderDate="2005-11-16">
<OrderDetail OrderID="10283" ProductID="99" Quantity="3"/>
</Order>
</Customer>
</ROOT>'
--Create an internal representation of the XML doc
EXEC sp_xml_preparedocument @int OUTPUT, @xmlORDER
-- OPENXML rowset provider.
SELECT *
FROM OPENXML (@int, '/ROOT/Customer',1)
WITH (CustomerID varchar(10),ContactName varchar(20))
The
code here takes the XML text and allows you to query and work with it
as if it were relational data. The output looks like this:
CustomerID ContactName
---------------------------
BRU Andrew Brust
ZAC Bill Zack
(2 row(s) affected)
You can optionally specify if you want OPENXML
to use element-based or attribute-based XML relational mapping between
the rowset columns and the XML nodes. There are two ways to control the
mapping. The first is to use the flags
parameter, which assumes that the XML nodes will map to corresponding
rowset columns with exactly the same name. You can also use the ColPattern parameter, an XPath expression that allows you to use a schema to perform the mapping as part of SchemaDeclaration in the WITH clause. The mapping specified in ColPattern overwrites the mapping specified by the flags parameter.
SQL Server 2005 introduces two enhancements to OPENXML, both involving the new XML data type. First, the XML data type is supported as an output column or an overflow column with the OPENXML statement. Second, you can pass an XML data type variable directly into sp_xml_preparedocument. Both of these enhancements enable you to more easily work with existing XML data in an XML column or created data using FOR XML TYPE.
XML Bulk Load
SQL Server 2000 XML
Bulk Load allows users to load large XML documents on the client side.
It works by reading the XML and producing SQL INSERTXML data type column.
statements that run on the client in batch. SQL Server 2005 greatly
enhances XML Bulk Load by allowing it to run on the server as well as to
load directly into an
Using the new XML Bulk Load requires using the system rowset provider function OPENROWSET and specifying the BULK provider:
Use AdventureWorks
--create a table with an xml column
create table tblxmlcustomers
(customer_id int primary key identity,
customer_xml xml not null)
--this file will load 1 record in (SINGLE_CLOB)
--for more records use a format file
insert into tblxmlcustomers
Select * from OPENROWSET
(Bulk 'C:\customer_01.xml',
SINGLE_CLOB) as xmldata
This example works by first creating a table that has an XML column and a primary key value. Then we use an INSERT statement that selects all of the data from the XML file using OPENROWSET. OPENROWSET uses the BULK provider and loads into the XML column of the tblxmlcustomers table the entire contents of the customer_01.xml file.