Logo
HOW TO
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
 
 
Windows Server

SQL Server 2012 : XML and the Relational Database - Shredding XML Using OPENXML

8/18/2013 9:32:53 PM

Up to this point, you have been using FOR XML to compose XML from rows of data, but what if you already have XML data and want to shred it back into relational data? SQL Server 2000 introduced a feature called OPENXML for this purpose. The OPENXML system function is designed for this purpose, and allows an XML document file to be shredded into T-SQL rows as we’ll explain next. Since the introduction of the native xml data type in SQL Server 2005, XQuery (covered in the next section) offers even more choices for extracting data from XML input.

To shred data XML into relational rows using OPENXML, you first create a handle to the XML document using the system stored procedure sp_xml_preparedocument. This system-stored procedure takes an XML document and creates a representation that you can reference using a special 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 OPENXML function just as you would SELECT from a table or a view. The code in Example 1 shows an example of OPENXML in action.

Example 1. Using FOR XML AUTO with ELEMENTS to produce element-based hierarchical XML.

DECLARE @handle int
DECLARE @OrdersXML varchar(max)
SET @OrdersXML = '
<Orders>
  <Customer CustomerID="HERBC" ContactName="Charlie Herb">
     <Order CustomerID="HERBC" EmployeeID="5" OrderDate="2011-11-04">
        <OrderDetail OrderID="10248" ProductID="16" Quantity="12"/>
        <OrderDetail OrderID="10248" ProductID="32" Quantity="10"/>
     </Order>
     <Order CustomerID="HERBC" EmployeeID="2" OrderDate="2011-11-16">
        <OrderDetail OrderID="10283" ProductID="99" Quantity="3"/>
     </Order>
  </Customer>
  <Customer CustomerID="HINKM" ContactName="Matt Hink">
     <Order CustomerID="HINKM" EmployeeID="3" OrderDate="2011-11-23">
        <OrderDetail OrderID="10283" ProductID="99" Quantity="3"/>
     </Order>
  </Customer>
</Orders>'

-- Get a handle onto the XML document
EXEC sp_xml_preparedocument @handle OUTPUT, @OrdersXML

-- Use the OPENXML rowset provider against the handle to parse/query the XML
SELECT *
 FROM OPENXML(@handle, '/Orders/Customer/Order')
 WITH (
  CustomerName varchar(max) '../@ContactName',
  OrderDate date)

This code allows you to query and work with the XML text as if it were relational data. The output looks like this:

CustomerName    OrderDate
--------------- --------------
Charlie Herb    2011-11-04
Charlie Herb    2011-11-16
Matt Hink       2011-11-23
This code first calls sp_xml_preparedocument to get a handle over the XML of customer orders. The handle is passed as the first parameter to OPENXML. The second parameter is an XPath expression that specifies the row pattern, and this identifies the nodes within the XML that are to be processed as rows. In this example, the XPath expression /Orders/Customer/Order drills down to the order level for each customer. There are three orders in the XML, so the query produces three rows with order dates (one for each order). The customer name is not available at the order level; it must be retrieved by reaching “up” one level for the Customer element’s ContactName attribute using a column pattern. This is achieved using the WITH clause. In this example, the CustomerName column is based on the column pattern ../@ContactName to obtain the ContactName attribute (remember that in XPath an @-symbol means “attribute”) from the parent Customer node (as denoted by the ../ path syntax).
Other -----------------
- Maintaining Windows Home Server 2011 : Defragmenting the System Drive, Reviewing Event Viewer Logs, Setting Up a Maintenance Schedule
- Maintaining Windows Home Server 2011 : Deleting Unnecessary Files from the System Drive
- Extending Dynamics GP with Free Software : Capturing Screenshots and sending an E-mail for support
- Extending Dynamics GP with Free Software : Extending Dynamics GP with the Support Debugging Tool, Coloring windows by company
- BizTalk 2006 : Getting Started with Pipeline Development (part 3) - Configuring Recoverable Interchanges, Using the Default Pipelines
- BizTalk 2006 : Getting Started with Pipeline Development (part 2) - Understanding Pipeline Execution, Understanding Interchanges
- BizTalk 2006 : Getting Started with Pipeline Development (part 1) - Pipeline Stages
- System Center Configuration Manager 2007 : Creating and Modifying Configurations (part 5) - External Authoring, Authoring with CP Studio
- System Center Configuration Manager 2007 : Creating and Modifying Configurations (part 4) - Console Authoring - Validation Criteria
- System Center Configuration Manager 2007 : Creating and Modifying Configurations (part 3) - Console Authoring - Settings
 
 
REVIEW
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
 
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
 
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Popular keywords
HOW TO Swimlane in Visio Visio sort key Pen and Touch Creating groups in Windows Server Raid in Windows Server Exchange 2010 maintenance Exchange server mail enabled groups Debugging Tools Collaborating
Top 10
- Microsoft Excel : How to Use the VLookUp Function
- Fix and Tweak Graphics and Video (part 3) : How to Fix : My Screen Is Sluggish - Adjust Hardware Acceleration
- Fix and Tweak Graphics and Video (part 2) : How to Fix : Text on My Screen Is Too Small
- Fix and Tweak Graphics and Video (part 1) : How to Fix : Adjust the Resolution
- Windows Phone 8 Apps : Camera (part 4) - Adjusting Video Settings, Using the Video Light
- Windows Phone 8 Apps : Camera (part 3) - Using the Front Camera, Activating Video Mode
- Windows Phone 8 Apps : Camera (part 2) - Controlling the Camera’s Flash, Changing the Camera’s Behavior with Lenses
- Windows Phone 8 Apps : Camera (part 1) - Adjusting Photo Settings
- MDT's Client Wizard : Package Properties
- MDT's Client Wizard : Driver Properties
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro