Skip to content

Latest commit

 

History

History
47 lines (40 loc) · 1.94 KB

column-names-with-the-path-specified-as-data.md

File metadata and controls

47 lines (40 loc) · 1.94 KB
title description author ms.author ms.reviewer ms.date ms.service ms.subservice ms.topic helpviewer_keywords
Column Names with the Path Specified as data()
Learn about XML queries containing column names with the path specified as data().
MikeRayMSFT
mikeray
randolphwest
05/05/2022
sql
xml
conceptual
names [SQL Server], columns with

Column names with the path specified as data()

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

If the path specified as column name is data(), the value is treated as an atomic value in the generated XML. A space character is added to the XML if the next item in the serialization is also an atomic value. This is useful when you're creating list typed element and attribute values. The following query retrieves the product model ID, name, and list of products in that product model.

USE AdventureWorks2022;
GO
SELECT ProductModelID       AS "@ProductModelID",
       Name                 AS "@ProductModelName",
      (SELECT ProductID AS "data()"
       FROM   Production.Product
       WHERE  Production.Product.ProductModelID =
              Production.ProductModel.ProductModelID
      FOR XML PATH (''))    AS "@ProductIDs"
FROM  Production.ProductModel
WHERE ProductModelID = 7
FOR XML PATH('ProductModelData');

The nested SELECT retrieves a list of product IDs. It specifies "data()" as the column name for product IDs. Because PATH mode specifies an empty string for the row element name, there's no row element generated. Instead, the values are returned as assigned to a ProductIDs attribute of the <ProductModelData> row element of the parent SELECT. This is the result:

<ProductModelData
  ProductModelID = "7"
  ProductModelName="HL Touring Frame"
  ProductIDs="885 887 888 889 890 891 892 893"
/>

See also