Example: Specifying a Root Element for the XML Generated by FOR XML
By specifying the
option in the
query, you can request a single, top-level
element for the resulting XML, as shown in this query. The argument specified for the
directive provides the root element name.
This is the result:
XML
Use RAW Mode with FOR XML
ROOT
FOR XML
ROOT
USE
AdventureWorks2022;
GO
SELECT
ProductModelID,
Name
FROM
Production.ProductModel
WHERE
ProductModelID
IN (122, 119, 115)
FOR
XML
RAW
, ROOT(
'MyRoot'
);
GO
<MyRoot>
<row
ProductModelID
=
"122"
Name
=
"All-Purpose Bike Stand"
/>
<row
ProductModelID
=
"119"
Name
=
"Bike Wash"
/>
<row
ProductModelID
=
"115"
Name
=
"Cable Lock"
/>
</MyRoot>