Aracılığıyla paylaş


xml ile iç içe for xml sorgular şekillendirme

Aşağıdaki örnek sorgu Production.Product almak için tablo ListPrice ve StandardCost değerleri, belirli bir ürün.Sorguyu ilgi çekici hale getirmek için her iki fiyatları döndürülür bir <Price> ve her öğe, <Price> öğesine sahip bir PriceType öznitelik.

Örnek

Bu XML'in beklenen şekli şöyledir:

<xsd:schema xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="https://schemas.microsoft.com/sqlserver/2004/sqltypes" targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet2" elementFormDefault="qualified">

<xsd:import namespace="https://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="https://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />

<xsd:element name="Production.Product" type="xsd:anyType" />

</xsd:schema>

<Production.Product xmlns="urn:schemas-microsoft-com:sql:SqlRowSet2" ProductID="520">

<Price PriceType="ListPrice">133.34</Price>

<Price PriceType="StandardCost">98.77</Price>

</Production.Product>

İç içe for xml sorgu budur:

USE AdventureWorks2008R2;
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;

Önceki sorgudan aşağıdakilere dikkat edin:

  • Dış deyim yapıları <Product> öğesi olan bir ProductID öznitelik ve iki <Price> alt öğeleri.

  • İki iki iç select deyimleri oluşturmak <Price> öğeleri, her biri bir PriceType öznitelik ve ürün fiyatını verir xml.

  • Sonuç xml şeklini açıklar satır içi xsd şeması dış deyim içinde xmlschema yönergesi oluşturur.

Sorgu ilginç hale getirmek için for xml sorgu yazın ve sonra aşağıdaki sorgu gösterildiği gibi xml yeniden şekillendirmek için bir XQuery sonucu karşı yaz:

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 
                                    <Price PriceType = "{local-name($p)}">
                                     { data($p) }
                                    </Price>
                                  ')
FROM Production.Product
WHERE ProductID = 520
FOR XML AUTO, TYPE;

Yukarıdaki örnek query() yöntem, xml veri türü iç for xml sorgu tarafından döndürülen xml sorgulamak ve oluşturması Beklenen sonuç.

Bu sonucu verir:

<Production.Product ProductID="520">

<Price PriceType="ListPrice">133.3400</Price>

<Price PriceType="StandardCost">98.7700</Price>

</Production.Product>

Ayrıca bkz.

Başvuru