Example: Specifying a Root Element for the XML Generated by FOR XML

 

Applies To: SQL Server 2016

By specifying the ROOT option in the FOR XML query, you can request a single, top-level element for the resulting XML, as shown in this query. The argument specified for the ROOT directive provides the root element name.

USE AdventureWorks2012;  
GO  
SELECT ProductModelID, Name   
FROM Production.ProductModel  
WHERE ProductModelID=122 or ProductModelID=119 or ProductModelID=115  
FOR XML RAW, ROOT('MyRoot')  
go  

This is the result:

<MyRoot>  
  <row ProductModelID="122" Name="All-Purpose Bike Stand" />  
  <row ProductModelID="119" Name="Bike Wash" />  
  <row ProductModelID="115" Name="Cable Lock" />  
</MyRoot>  

Use RAW Mode with FOR XML

Community Additions

ADD
Show: