Raw
FOR XML RAW is similar to FOR XML AUTO but with several differences. First, FOR XML AUTO does not enable you to alter the name of the elements in the resulting XML. Also, as you saw in the previous section, FOR XML AUTO names the elements after the name of the table or alias.
For example, the following code illustrates a simple FOR XML RAW clause:
SELECT CustomerID, OrderNumber, OrderDate
FROM Orders
FOR XML RAW
/*
<row CustomerID="1" OrderNumber="10001" OrderDate="2011-06-15T00:00:00" />
<row CustomerID="2" OrderNumber="10002" OrderDate="2011-06-16T00:00:00" />
<row CustomerID="1" OrderNumber="10003" OrderDate="2011-06-17T00:00:00" />
<row CustomerID="2" OrderNumber="10004" OrderDate="2011-06-18T00:00:00" />
*/
By default, FOR XML RAW generates elements named <row> but enables you to rename it if you want via an optional element name of the RAW directive:
SELECT CustomerID, OrderNumber, OrderDate
FROM Orders
FOR XML RAW('Order')
/*
<Order CustomerID="1" OrderNumber="10001" OrderDate="2011-06-15T00:00:00" />
<Order CustomerID="2" OrderNumber="10002" OrderDate="2011-06-16T00:00:00" />
<Order CustomerID="1" OrderNumber="10003" OrderDate="2011-06-17T00:00:00" />
<Order CustomerID="2" OrderNumber="10004" OrderDate="2011-06-18T00:00:00" />
*/
In the following example, notice that table names in the query are both aliased, but because no name is specified in the RAW directive, each row is still named <row>.
SELECT o.OrderNumber, o.OrderDate, c.Name
FROM Orders o
INNER JOIN Customer c ON o.CustomerID = c.CustomerID
FOR XML RAW
/*
<row OrderNumber="10001" OrderDate="2011-06-15T00:00:00" Name="Scott" />
<row OrderNumber="10002" OrderDate="2011-06-16T00:00:00" Name="Adam" />
<row OrderNumber="10003" OrderDate="2011-06-17T00:00:00" Name="Scott" />
<row OrderNumber="10004" OrderDate="2011-06-18T00:00:00" Name="Adam" />
*/
The RAW directive also enables you to specify an optional ROOT directive (similar to the AUTO directive), which generates a root element with the specified name.
SELECT o.OrderNumber, o.OrderDate, c.Name
FROM Orders o
INNER JOIN Customer c ON o.CustomerID = c.CustomerID
FOR XML RAW('Order'), ROOT('Orders')
/*
<Orders>
<Order OrderNumber="10001" OrderDate="2011-06-15T00:00:00" Name="Scott" />
<Order OrderNumber="10002" OrderDate="2011-06-16T00:00:00" Name="Adam" />
<Order OrderNumber="10003" OrderDate="2011-06-17T00:00:00" Name="Scott" />
<Order OrderNumber="10004" OrderDate="2011-06-18T00:00:00" Name="Adam" />
</Orders>
/*
If you specify the ROOT directive without including a name for the ROOT element, a top-level root element named <root> generates.
The RAW directive also accepts the ELEMENTS directive:
SELECT o.OrderNumber, o.OrderDate, c.Name
FROM Orders o
INNER JOIN Customer c ON o.CustomerID = c.CustomerID
FOR XML RAW('Order'), ROOT('Orders'), ELEMENTS
/*
<Orders>
<Order>
<OrderNumber>10001</OrderNumber>
<OrderDate>2011-06-15T00:00:00</OrderDate>
<Name>Scott</Name>
</Order>
<Order>
<OrderNumber>10002</OrderNumber>
<OrderDate>2011-06-16T00:00:00</OrderDate>
<Name>Adam</Name>
</Order>
<Order>
<OrderNumber>10003</OrderNumber>
<OrderDate>2011-06-17T00:00:00</OrderDate>
<Name>Scott</Name>
</Order>
<Order>
<OrderNumber>10004</OrderNumber>
<OrderDate>2011-06-18T00:00:00</OrderDate>
<Name>Adam</Name>
</Order>
</Orders>
/*