Aracılığıyla paylaş


İç içe FOR XML sorguları ile XML'i biçimlendirin

Şunlar için geçerlidir:SQL ServerAzure SQL VeritabanıAzure SQL Yönetilen ÖrneğiMicrosoft Fabric'te SQL veritabanı

Aşağıdaki örnek, belirli bir ürünün Production.Product ve ListPrice değerlerini almak için StandardCost tablosunu sorgular. Sorguyu ilginç hale getirmek için her iki fiyat da bir <Price> öğesinde döndürülür ve her <Price> öğesi bir PriceType özniteliğine sahiptir.

Örnek

Bu, XML'nin beklenen şeklidir:

<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 xmlns="" PriceType="ListPrice">133.34</Price>
  <Price xmlns="" PriceType="StandardCost">98.77</Price>
</Production.Product>

Bu, iç içe YERLEŞTIRILMIŞ FOR XML sorgusudur:

USE AdventureWorks2022;
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ış SELECT deyimi, <Product> özniteliğine ve iki alt öğeye sahip <Price> öğesini oluşturur.

  • İki iç SELECT deyimi, her biri <Price> özniteliğine ve ürün fiyatını döndüren XML'e sahip iki öğesi oluşturur.

  • Dış SELECT deyimindeki XMLSCHEMA yönergesi, sonuçta elde edilen XML'nin şeklini açıklayan satır içi XSD şemasını oluşturur.

Sorguyu ilginç hale getirmek için FOR XML sorgusunu yazabilir ve ardından aşağıdaki sorguda gösterildiği gibi XML'yi yeniden şekillendirmek için sonuda bir XQuery yazabilirsiniz:

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;

Önceki örnek, iç FOR XML sorgusu tarafından döndürülen XML'yi sorgulamak ve beklenen sonucu oluşturmak için query() veri türünün yöntemini kullanır.

Sonuç şu şekildedir:

<Production.Product ProductID="520">
  <Price PriceType="ListPrice">133.3400</Price>
  <Price PriceType="StandardCost">98.7700</Price>
</Production.Product>

Ayrıca bkz.