Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Browse by Tags · View All
XML 112
TSQL 69
XQuery 69
XQuery Functions 67
XQuery Training 65
XQuery in TSQL 64
XQuery Tutorial 63
SQL Server XQuery 63
XQuery-Labs 57
BRH 38

Archive · View All
September 2008 32
August 2008 30
July 2008 21
August 2009 19
June 2009 19
May 2010 18
January 2009 15
January 2010 14
October 2008 14
June 2008 13

XQuery Lab 57 – Getting Started with OPENXML

Jun 17 2010 8:25AM by Jacob Sebastian   

This post intends to help you get started with OPENXML() function. OPENXML() lets you shred an XML document or fragment into a result set.

Though OPENXML() has got no direct relationship with XQuery, I thought of including it in the XQuery Labs series for completeness. Just like XQuery, OPENXML() can also be used to query an XML document (with some limitations).

Reading values from elements

The following example shows a basic example that read the values of elements from an XML document using OPENXML()

DECLARE @x VARCHAR(8000)
SET @x = 
  '<authors>
    <author>
        <firstname>Michael</firstname>
        <lastname>Howard</lastname>
    </author>
    <author>
        <firstname>David</firstname>
        <lastname>LeBlanc</lastname>
    </author>
   </authors>'
   
DECLARE @h INT
EXECUTE sp_xml_preparedocument @h OUTPUT, @x 

SELECT * 
FROM OPENXML(@h, '/authors/author', 2)
WITH(
	firstname VARCHAR(20),
	lastname VARCHAR(20)
)

EXECUTE sp_xml_removedocument @h 

/*
firstname            lastname
-------------------- --------------------
Michael              Howard
David                LeBlanc
*/

The third argument passed into the OPENXML() function (“2” in this example) indicates that we wanted to read the values of elements from the XML document.

Reading values from attributes

You can read attribute values by passing “1” instead of “2”. Here is another example that reads attribute values.

DECLARE @x VARCHAR(8000)
SET @x = 
  '<authors>
    <author fname="Michael" lname="Howard"/>
    <author fname="David" lname="LeBlanc" />
   </authors>'
   
DECLARE @h INT
EXECUTE sp_xml_preparedocument @h OUTPUT, @x 

SELECT * 
FROM OPENXML(@h, '/authors/author', 1)
WITH(
	fname VARCHAR(20),
	lname VARCHAR(20)
)

EXECUTE sp_xml_removedocument @h 

/*
firstname            lastname
-------------------- --------------------
Michael              Howard
David                LeBlanc
*/

Reading both elements and attributes

The following example reads values from elements and attributes in a single query.

DECLARE @x VARCHAR(8000)
SET @x = 
  '<authors>
    <author id="101">Jacob</author>
    <author id="102">Steve</author>
   </authors>'
   
DECLARE @h INT
EXECUTE sp_xml_preparedocument @h OUTPUT, @x 

SELECT * 
FROM OPENXML(@h, '/authors/author', 1)
WITH(
	id INT '@id',
	author VARCHAR(20) '.'
)

EXECUTE sp_xml_removedocument @h 

/*
id          author
----------- --------------------
101         Jacob
102         Steve
*/

Notes

  1. OPENXML() was first introduced in SQL Server 2000. XQuery support and XML data type were added only in SQL Server 2005.
  2. OPENXML() uses MSXML Parser internally
  3. In many cases, OPENXML() is found to be more efficient than XQuery when processing Large XML documents.
  4. OPENXML() cannot be used as part of a SET operation, nor can be called from a function. It needs a three step approach – prepare document, query document and remove document
  5. OPENXML() is found to be more memory intensive in most cases.
  6. If you forget to call sp_xml_removedocument, you might end up with a memory leak.

View All Labs: XQuery Labs - A Collection of XQuery Sample Scripts and Tutorials

Visit the XML Resource Center for more XML articles and tutorials.

Tags: XML, XQuery, TSQL, SQLSERVER, BRH, XQuery Functions, xquery labs, sp_xml_preparedocument, OPENXML, Database, DBA, #XML, #XQUERY, sp_xml_removedocument, #SQLSERVER, SQL Server XQuery, XQuery in TSQL, XQuery Training, XQuery Tutorial,


Jacob Sebastian
1 · 100% · 32004
2
 
0
Lifesaver
 
0
Refreshed
 
 
0
Incorrect



Submit

3  Comments  

  • Thank you for sharing!

    commented on Mar 2 2012 11:17PM
    Guru Samy
    9 · 16% · 4992
  • What should you do if you have & character in the data?

    commented on Jun 14 2012 1:53PM
    Naomi
    33 · 6% · 1774
  • First of all, & is not permitted in an XML document. So if your incoming XML document needs to place &, it has to be encoded as &amp;. In such a case, the processing will happen normally and your output will show & correctly

    commented on Jun 15 2012 9:57AM
    Jacob Sebastian
    1 · 100% · 32004

Your Comment


Sign Up or Login to post a comment.

"XQuery Lab 57 – Getting Started with OPENXML" rated 5 out of 5 by 2 readers
XQuery Lab 57 – Getting Started with OPENXML , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]