The FOR XML Clause

The FOR XML clause in the SELECT statement has been provided to support retrieving results in XML format. Queries can therefore be created to return a resultset in the form of an XML document instead of a standard row set.

When writing a query, the FOR XML clause in a SELECT statement enables you to specify that the resultset should be returned as an XML document and can be specified in one of three modes:

FOR XML RAW
FOR XML AUTO
FOR XML EXPLICIT

FOR XML RAW

This mode takes the query result, and transforms each row in the resultset into XML elements with a generic identifier "row" as the element tag. An example of this is seen in the following code:

 - <root> <row name="John" telephone1="777 7777" telephone2="999" mobile="010" ...

Get Writing Stored Procedures for Microsoft SQL Server 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.