以 data() 指定路徑的資料行名稱Column Names with the Path Specified as data()

適用範圍:Applies to: 是SQL ServerSQL Server (所有支援的版本) yesSQL ServerSQL Server (all supported versions) 是Azure SQL DatabaseAzure SQL DatabaseYesAzure SQL DatabaseAzure SQL Database適用範圍:Applies to: 是SQL ServerSQL Server (所有支援的版本) yesSQL ServerSQL Server (all supported versions) 是Azure SQL DatabaseAzure SQL DatabaseYesAzure SQL DatabaseAzure SQL Database

如果以資料行名稱指定的路徑是 "data()",則在產生的 XML 中會將其值視為不可部分完成值。If the path specified as column name is "data()", the value is treated as an atomic value in the generated XML. 如果序列化的下一個項目也是不可部分完成值,就會在 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 are 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 AdventureWorks2012;  
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');  

巢狀 SELECT 會擷取產品識別碼的清單。The nested SELECT retrieves a list of product IDs. 它會將 "data()" 指定為產品識別碼的資料行名稱。It specifies "data()" as the column name for product IDs. 因為 PATH 模式會為資料列元素名稱指定空白字串,所以不會產生任何資料列元素。Because PATH mode specifies an empty string for the row element name, there is no row element generated. 而是會以指派給父項 SELECT 中 <ProductModelData> 資料列項目的 ProductID 屬性來傳回值。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

搭配 FOR XML 使用 PATH 模式Use PATH Mode with FOR XML