Please check my previous article @
The for Section of the select Statement
The for section of the select statement is used to modify the relational structure of an XML document. Compared to the previous version of SQL Server, the capabilities of this option have been greatly expanded.
The general format of using the for option is the following:
[ for { browse | <xml> } ]
<xml> ::=
xml
{
{ raw [ ( 'ElementName' ) ] | auto }
[ <CoirmonDirectives>
[ , { xmldata | xmlschema [ { 'TargetNameSpaceURI' ) ] } ]
[ , elements [ xsinil | absent ]
]
| explicit
[ <ComomonDirectives>
[ , xmldata ]
]
| path [ ( 'ElementName' ) ]
[ <CommonDirectives>
[ , elements [ xsinil | absent ] ]
]
}
<CommonDirectives> :: =
[ , binary base64 ]
[ , type ]
[ , root [ ( 'RootName' ) ] ]
The meaning of each element in the preceding description is as follows:
- Browse: This option has no relation to XML. It specifies that data can be updated when viewed with applications that use DB-Library.
- Xml: The query result is to be converted to an XML document.
- Raw [('ElementName')]: A data set is to be returned, in which each row is an element named <ElementName /> or <row />, if the parentheses are omitted.
- Auto: An XML document is formed, in which the name of a table or a view is used as the name of the element representing a row of a result data set.
- Explicit: The structure of the XML document is to be specified explicitly when defining the columns of the result data set.
- Xml data: An XML-data reduced (XDR) schema is to be joined to the XML document.
Note: An XDR schema describes elements that an XML document may contain and the attributes that can be associated with the elements. It also describes the structure of an XML document, for example, specifying the daughter elements, their order, and number. A schema determines whether an element is empty or contains text. It can also hold the default values of attributes.
- Xmlschema [ ( 'TargetNameSpaceURI' ) ]: Specifies that an XML schema definition is to be created in the XML document.
f0_i DB-Library for C is an application programming interface consisting of C functions and macros that allow an application to interact with Microsoft SQL Server 2000. Included are functions that send Transact-SQL statements to SQL Server and functions that process the results of those statements.
- Elements: The columns should be represented not by attributes but by daughter elements.
xsinil " A column that can assume NULL value should be represented by an element with the xsi:nil attribute, whose value becomes TRUE if the column value becomes NULL.
absent " If the column value is NULL, the corresponding element will not be added to the XML document.
path [ ( 'ElementName' ) ] "The path to the data for XML output.
binary base64 " The data should be returned in the binary format.
type " The query result should be returned as XML data (not as text).
root [ ( 'RootName' ) ] "A root element can be added to an XML document.
Now consider some examples of using the for option for generating an XML document.
Suppose that there is a table containing information about books in three columns: <id, author, title>. Execute the below query:
select * from dbo.books for xml path ('book')
When executing a query in the SqlQuery window, Microsoft SQL Server Management Studio allows the resulting XML document to be viewed.
The output of the above query is:
<book>
<id> 1 </id>
<author> Charles Dickens </author>
<title> Oliver Twist </title>
</book>
<book>
<id> 2 </id>
<author> Stephen King </author>
<title> The Stand </title>
</book>
<book>

Join the conversation! Your thoughts help the community grow.