--- title: "Shape XML with Nested FOR XML Queries | Microsoft Docs" ms.custom: "" ms.date: "06/13/2017" ms.prod: "sql-server-2014" ms.reviewer: "" ms.technology: xml ms.topic: conceptual helpviewer_keywords: - "FOR XML query" - "queries [XML in SQL Server], nested FOR XML" - "XML [SQL Server], FOR XML queries" ms.assetid: 8dc42c05-16e8-4b7b-a5d3-550b55acae26 author: MightyPen ms.author: genemi manager: craigg --- # Shape XML with Nested FOR XML Queries The following example queries the `Production.Product` table to retrieve the `ListPrice` and `StandardCost` values of a specific product. To make the query interesting, both prices are returned in a <`Price`> element, and each <`Price`> element has a `PriceType` attribute. ## Example This is the expected shape of the XML: ``` 133.34 98.77 ``` This is the nested FOR XML query: ``` USE AdventureWorks2012; GO SELECT Product.ProductID, (SELECT 'ListPrice' as PriceType, CAST(CAST(ListPrice as NVARCHAR(40)) as XML) FROM Production.Product Price WHERE Price.ProductID=Product.ProductID FOR XML AUTO, TYPE), (SELECT 'StandardCost' as PriceType, CAST(CAST(StandardCost as NVARCHAR(40)) as XML) FROM Production.Product Price WHERE Price.ProductID=Product.ProductID FOR XML AUTO, TYPE) FROM Production.Product WHERE ProductID=520 for XML AUTO, TYPE, XMLSCHEMA ``` Note the following from the previous query: - The outer SELECT statement constructs the <`Product`> element that has a **ProductID** attribute and two <`Price`> child elements. - The two inner SELECT statements construct two <`Price`> elements, each with a **PriceType** attribute and XML that returns the product price. - The XMLSCHEMA directive in the outer SELECT statement generates the inline XSD schema that describes the shape of the resulting XML. To make the query interesting, you can write the FOR XML query and then write an XQuery against the result to reshape the XML, as shown in the following query: ``` SELECT ProductID, ( SELECT p2.ListPrice, p2.StandardCost FROM Production.Product p2 WHERE Product.ProductID = p2.ProductID FOR XML AUTO, ELEMENTS XSINIL, type ).query(' for $p in /p2/* return { data($p) } ') FROM Production.Product WHERE ProductID = 520 FOR XML AUTO, TYPE ``` The previous example uses the `query()` method of the `xml` data type to query the XML returned by the inner FOR XML query and construct the expected result. This is the result: ``` 133.3400 98.7700 ``` ## See Also [Use Nested FOR XML Queries](use-nested-for-xml-queries.md)