Örnek: ÖĞE yönergesi ve varlık kodlama belirtme
Bu örnek arasındaki farkı gösterir ELEMENT ve XML yönergeleri.The ELEMENT directive entitizes the data, but the XML directive does not.The <Summary> element is assigned XML, <Summary>This is summary description</Summary>, in the query.
Bu sorgu göz önünde bulundurun:
USE AdventureWorks2008R2;
GO
SELECT 1 as Tag,
0 as Parent,
ProductModelID AS [ProductModel!1!ProdModelID],
Name As [ProductModel!1!Name],
NULL AS [Summary!2!SummaryDescription!ELEMENT]
FROM Production.ProductModel
WHERE ProductModelID=19
UNION ALL
SELECT 2 AS Tag,
1 AS Parent,
ProductModelID,
NULL,
'<Summary>This is summary description</Summary>'
FROM Production.ProductModel
WHERE ProductModelID=19
FOR XML EXPLICIT;
Sonuç budur.Özet açıklaması sonucu entitized.
<ProductModel ProdModelID="19" Name="Mountain-100">
<Summary>
<SummaryDescription><Summary>This is summary description</Summary></SummaryDescription>
</Summary>
</ProductModel>
Şimdi belirtirseniz, XML sütun adı yönergesinde Summary!2!SummaryDescription!XML, yerine ELEMENT yönergesi içeriyorsa, alırsınız Özet açıklaması entitization olmadan
<ProductModel ProdModelID="19" Name="Mountain-100">
<Summary>
<SummaryDescription>
<Summary>This is summary description</Summary>
</SummaryDescription>
</Summary>
</ProductModel>
Statik bir xml değeri atamak yerine, aşağıdaki sorgu kullanır query() yöntem, xml türü ürün modeli Özet açıklamasından CatalogDescription sütun almak için xml türü.Sonuç olarak biliniyor çünkü xml türü, hiçbir entitization uygulanır.
SELECT 1 as Tag,
0 as Parent,
ProductModelID AS [ProductModel!1!ProdModelID],
Name AS [ProductModel!1!Name],
NULL AS [Summary!2!SummaryDescription]
FROM Production.ProductModel
WHERE CatalogDescription IS NOT NULL
UNION ALL
SELECT 2 AS Tag,
1 AS Parent,
ProductModelID,
Name,
(SELECT 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],Tag
FOR XML EXPLICIT;