Home > SQL Server Tips > Database Development > Processing XML files with SQL Server functions
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE DEVELOPMENT

Processing XML files with SQL Server functions


Denny Cherry
02.16.2009
Rating: -4.67- (out of 5)


Expert advice on database administration
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


Since the release of SQL Server 2000, processing XML data has been a topic of discussion among database administrators. It is often used when a front-end application passes an XML document to a stored procedure within an input parameter. Sometimes, however, you encounter a folder containing a set of XML files that needs to be loaded into the database and then processed into SQL Server tables. This is more difficult, and documentation on how to do so is sparse.

You can use several techniques, most of which are quite complex. I have used SQL Server Integration Services (SSIS), Data Transformation Services (DTS) and a self-written Windows application to read and load files. My favorite technique, however, is to use the OPENROWSET function. This function enables a good deal of flexibility, because you can control the entire process from each end within your T-SQL stored procedure.

The first step of the process is to create a table with a single column that uses the XML data type. A temporary table also works:

CREATE TABLE #WorkingTable
(Data XML)

With the OPENROWSET function, you then load the data into a single row of the table:

INSERT INTO #WorkingTable
SELECT * FROM OPENROWSET (BULK 'D:\Temp\Sample.xml', SINGLE_BLOB) AS data

You can now place the value from the Data column of the #WorkingTable table into a variable, call on the OPENXML function to parse the XML document and use it as necessary. In this case, the XML document is very basic:

The OPENXML code necessary to read this data from the temporary table should look something like this:

DECLARE @XML AS XML, @hDoc AS INT
SELECT @XML = Data FROM #WorkingTable

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML
SELECT *
FROM OPENXML(@hDoc, '/root/data', 1)
WITH (Col1 VARCHAR(5) '@parm1',
Col2 VARCHAR(5) '@parm2')

EXEC sp_xml_removedocument @hDoc

The resulting output from this query will look something like this:

This is the basic syntax of all your OPENXML statements. From here, you can add greater complexity to /root/data, @parm1 and the other XPath expressions. This allows you to process data from more complex XML documents. Here is an example of a more complex XML document, with invoice data:

As you can see, we have a three-level document with needed data in the Customer ID, Order ID and Item ID nodes. In this sample code, you can see that we start with the Item ID node and work our way back to the Customer ID and Order ID values:

DECLARE @XML AS XML, @hDoc AS INT
SELECT @XML = Data FROM #WorkingTable

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

SELECT *
FROM OPENXML(@hDoc, '/root/Customer/Order/Item')
WITH (CustomerId INT '../../@ID',
OrderId INT '../@ID',
ItemId INT '@ID',
Qty INT '@Qty')

EXEC sp_xml_removedocument @hDoc

A record set like this is then produced:

If we were to take this same XML document and add an order for a second customer, we would see an output like this:

As I said, we started at the Item node of the XML document. If we started at the Customer node of the document and worked our way down the document, our output would not be correct. We would instead get only the first item for each Customer ID in the document.

This OPENXML function gives us the following incorrect recordset:

SELECT *
FROM OPENXML(@hDoc, '/root/Customer')
WITH (CustomerId INT '@ID',
OrderId INT 'Order/@ID',
ItemId INT 'Order/Item/@ID',
Qty INT 'Order/Item/@Qty')

In this article, we have discussed basic examples involving the use of XPath. For more comprehensive examples, you can refer to Microsoft's XPath syntax options to leverage the power of the XML engine in T-SQL statements.

ABOUT THE AUTHOR:   
Denny Cherry has more than a decade of experience managing SQL Server, including MySpace.com's 175 million-plus user installation, one of the largest in the world. Cherry's areas of expertise include systems architecture, performance tuning, replication and troubleshooting. He uses these skills on a regular basis in his current role as a senior database administrator and architect at Awareness Technologies. Cherry holds several Microsoft certifications related to SQL Server and is a Microsoft MVP. He is a member of PASS and Quest Software's Association of SQL Server Experts and has written numerous technical articles on SQL Server management.
Check out his blog: SQL Server with Mr. Denny.


Rate this Tip
To rate tips, you must be a member of SearchSQLServer.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
XML in SQL Server
Top 10 SQL Server development tips of 2008
Manage traces in SQL Server 2005 Analysis Services with XMLA commands
Create a computed column in SQL Server using XML data
Create DDL table in SQL Server 2005 to audit DDL trigger activity
Retrieve XML data values with XQuery in SQL Server 2005
XML data type in SQL Server 2005 vs. VARCHAR (MAX)
T-SQL commands vs. XML AUTO in SQL Server
Basic Transact-SQL programming constructs: 15 tips, 15 minutes
T-SQL identifiers
Character string data types

Database Development
Using DELETE and TRUNCATE TABLE statements to delete data in SQL Server
Speed up reports in SQL Server Reporting Services with caching
Data Transformation Services vs. SSIS: The key differences
Working with IntelliSense in SQL Server 2008 Management Studio
Top tips and tricks for SQL Server database development
Managing the development lifecycle with Visual Studio Team System 2008
A first look at Visual Studio Team System 2008 Database Edition
How to create a SQL inner join and outer join: Basics to get started
New datetime data types in SQL Server 2008 offer flexibility
Using DATEADD and DATEDIFF to calculate SQL Server datetime values

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Commerce XML  (SearchSQLServer.com)
DSTP  (SearchSQLServer.com)
XQuery  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SQL Server Development - .NET, C#, T-SQL, Visual Basic
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts