| title | Specify a root element for use with FOR XML | ||
|---|---|---|---|
| description | View an example query that specifies the ROOT option of the FOR XML clause to request a single top-level element in the resulting XML. | ||
| author | MikeRayMSFT | ||
| ms.author | mikeray | ||
| ms.reviewer | randolphwest | ||
| ms.date | 05/05/2022 | ||
| ms.service | sql | ||
| ms.subservice | xml | ||
| ms.topic | how-to | ||
| ms.custom |
|
||
| helpviewer_keywords |
|
[!INCLUDE SQL Server Azure SQL Database Azure SQL Managed Instance FabricSQLDB]
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 AdventureWorks2022;
GO
SELECT ProductModelID, Name
FROM Production.ProductModel
WHERE ProductModelID IN (122, 119, 115)
FOR XML RAW, ROOT('MyRoot');
GOThis 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>