Aracılığıyla paylaş


Örnek: GİZLE yönerge belirtme

Bu örnek kullanımını göstermektedir gizleme yönergesi. Sorguyu sorgu tarafından döndürülen evrensel tablodaki satırları sipariş öznitelik dönmek için istediğiniz zaman bu yönerge yararlıdır, ancak nihai sonuç xml belgesinde bu öznitelik istemiyorum.

Bu sorgu, bu xml oluşturur:

<ProductModel ProdModelID="19" Name="Mountain-100">
  <Summary>
    <SummaryDescription>
           <Summary> element from XML stored in CatalogDescription column
    </SummaryDescription>
  </Summary>
</ProductModel>

Bu sorgu istediğiniz xml oluşturur. Sorgu sütun adlarını etiket değer olarak 1 ve 2 sahip iki sütun gruplarını tanımlar.

Bu sorguyu kullanır query() yöntemi (xml veri türü) , xml CatalogDescription sütun sorgu için veri türü xml türü Özet açıklaması almak için. Ayrıca, sorgu kullanır value() yöntemi (xml veri türü) , xml CatalogDescription sütun ProductModelID değerini almak için veri türü. Bu değer, sonuç xml gerekli değildir ancak sonuç satır kümesi sıralamak için gereklidir. Bu nedenle, sütun adı, [Summary!2!ProductModelID!HIDE], içerir gizleme yönergesi. Bu sütunu select deyiminde dahil değilse satır kümesi tarafından sıralamak gerekir [ProductModel!1!ProdModelID]ve [Summary!2!SummaryDescription]yani xml türü ve bunu kullanamaz xml türü sütunu order by. Bu nedenle, ek [Summary!2!ProductModelID!HIDE]sütunu eklenir ve sonra order by yan tümcesinde belirtilen.

USE AdventureWorks2012;
GO
SELECT  1 as Tag,
        0 as Parent,
        ProductModelID     as [ProductModel!1!ProdModelID],
        Name               as [ProductModel!1!Name],
        NULL               as [Summary!2!ProductModelID!hide],
        NULL               as [Summary!2!SummaryDescription]
FROM    Production.ProductModel
WHERE   CatalogDescription is not null
UNION ALL
SELECT  2 as Tag,
        1 as Parent,
        ProductModelID,
        Name,
        CatalogDescription.value('
         declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
       (/PD:ProductDescription/@ProductModelID)[1]', 'int'),
        CatalogDescription.query('
         declare namespace pd="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
         /pd:ProductDescription/pd:Summary')
FROM    Production.ProductModel
WHERE   CatalogDescription is not null
ORDER BY [ProductModel!1!ProdModelID],[Summary!2!ProductModelID!hide]
FOR XML EXPLICIT
go

Sonuç şudur:

<ProductModel ProdModelID="19" Name="Mountain-100">
  <Summary>
    <SummaryDescription>
      <pd:Summary xmlns:pd="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription" >
        <p1:p xmlns:p1="http://www.w3.org/1999/xhtml">Our top-of-the-line competition mountain bike. Performance-enhancing options include the innovative HL Frame, super-smooth front suspension, and traction for all terrain. </p1:p>
      </pd:Summary>
    </SummaryDescription>
  </Summary>
</ProductModel>

Ayrıca bkz.

Başvuru

Kullanımı ile for xml Açık modu