Skip to content

Latest commit

 

History

History
45 lines (37 loc) · 1.33 KB

example-specifying-a-root-element-for-the-xml-generated-by-for-xml.md

File metadata and controls

45 lines (37 loc) · 1.33 KB
title description author ms.author ms.reviewer ms.date ms.service ms.subservice ms.topic helpviewer_keywords
Specify a root element for use with FOR XML
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.
MikeRayMSFT
mikeray
randolphwest
05/05/2022
sql
xml
how-to
RAW mode, specifying root element example
RAW mode, with FOR XML example

Example: Specify a root element for the XML generated by FOR XML

[!INCLUDE SQL Server Azure SQL Database Azure SQL Managed Instance]

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.

Example

USE AdventureWorks2022;
GO
SELECT ProductModelID, Name
FROM Production.ProductModel
WHERE ProductModelID IN (122, 119, 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>

See also