Shredding XML Using OPENXML

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 ...

Get Programming Microsoft® SQL Server® 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.