first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

SQL Server 2005: Flippant Remarks about the New FOR XML PATH() Syntax

The FOR XML PATH() syntax uses standard XPath queries to replace the proprietary and often extremely confusing syntax of the FOR XML EXPLICIT syntax. The following code block shows how this works:

DECLARE
    @d1 XML
,   @d2 XML

SET @d1 = '<one/>'

SET @d2 = '<two/>'

SELECT
    @d1 "node()"
,   @d2 "node()"
FOR XML PATH('root')

where node() is a standard XPath function. This SELECT statement returns:

<root>
  <one />
  <two />
</root>

You can see how the pattern above can be used to assemble arbitrary fragments of XML into one document. This use of native SQL 2005 XML data type eliminates the need for UNION queries! This pattern coupled with the new TYPE directive in the FOR XML clause yields almost unlimited flexibility!

For more information, see:

“What's New in FOR XML in Microsoft SQL Server 2005”
http://msdn2.microsoft.com/en-us/library/ms345137.aspx

and

“Bak2Basics: Learn T-SQL - FOR XML”
http://blah.winsmarts.com/2007-2-Bak2Basics__Learn_T-SQL_-_FOR_XML.aspx
mod date: 2007-11-08T01:39:25.000Z