Using XML With SQL Server 2005
Using XML With SQL Server 2005
www.pdfslibrary.com
Contents
Using XML ............................................................................................................................................................................. 3
Retrieving Data in Generic Row Elements .......................................................................................................................... 3
FOR XML RAW................................................................................................................................................................. 3
Retrieving Data as Elements............................................................................................................................................... 3
Retrieving Data by Using a Root Element and a Customized Row Element Name ............................................................... 4
Retrieving Nested Data by Using AUTO Mode .................................................................................................................... 4
Retrieving Data as Elements............................................................................................................................................... 4
EXPLICIT Mode Queries - Defining Column Mappings in a Universal Table.......................................................................... 5
Retrieving Data by Using PATH Mode................................................................................................................................. 7
Retrieving Data by Using PATH Mode - Modifying the Row Element Name ........................................................................ 8
Nested XML - Using TYPE to Return the xml Data Type in a Subquery ................................................................................ 8
www.pdfslibrary.com
Page 2 of 9
Using XML with SQL Server 2005
Using XML
The following example shows how you can retrieve an XML fragment containing order data by using a FOR XML query in
RAW mode.
www.pdfslibrary.com
Page 3 of 9
Using XML with SQL Server 2005
Retrieving Data by Using a Root Element and a Customized Row Element Name
The following example shows how you can retrieve the same data by using a root element specified with the ROOT option
and modify the row element name by using the RAW mode optional argument.
www.pdfslibrary.com
Page 4 of 9
Using XML with SQL Server 2005
As for RAW mode, you can also use the ROOT option, as shown in the following example.
www.pdfslibrary.com
Page 5 of 9
Using XML with SQL Server 2005
www.pdfslibrary.com
Page 6 of 9
Using XML with SQL Server 2005
www.pdfslibrary.com
Page 7 of 9
Using XML with SQL Server 2005
Retrieving Data by Using PATH Mode - Modifying the Row Element Name
The following example shows how you can use the optional ElementName argument to the PATH mode query to modify the
default row element name.
Nested XML - Using TYPE to Return the xml Data Type in a Subquery
SQL Server 2005 includes the xml data type. Specifying the TYPE directive in a FOR XML query returns the results as an xml
value instead of as a varchar string. The most significant impact of this is the ability to nest FOR XML queries to return
multilevel XML results in AUTO and RAW mode queries. The following example shows how to use the TYPE directive to nest
FOR XML queries.
www.pdfslibrary.com
Page 8 of 9
Using XML with SQL Server 2005
www.pdfslibrary.com
Page 9 of 9