Database Reference
In-Depth Information
Figure 7-1.
Using FOR XML RAW
How It Works
RAW
mode produces very “raw” XML. It turns each row in the result set into an empty XML
row
element
and uses an attribute for each of the column values, using the alias names we specified in the query as
the attribute names. It produces a string composed of all the elements.
RAW
mode doesn't produce an XML document, since it has as many root elements (
raw
) as there are
rows in the result set, and an XML document can have only one root element.
Try It: Using FOR XML RAW (Element-centric)
Changing the formatting from attribute-centric (as shown in the previous example) to element-centric
means that a new element will be created for each column. To achieve this, you need to add the
ELEMENTS
keyword after the
FOR XML RAW
clause, as shown in the following query.
1.
Replace the existing query in query pane with the following query, and click
Execute:
select Person.Contact.Title, Person.Contact.FirstName, Person.Contact.LastNamefrom
Person.Contact
where Person.Contact.Title ='Mr.'
for xml raw, elements