예: ELEMENT 지시문 및 엔터티 인코딩 지정

적용 대상:SQL ServerAzure SQL DatabaseAzure SQL Managed Instance

이 예제에서는 ELEMENT 지시문과 XML 지시문의 차이점을 보여 줍니다. ELEMENT 지시문은 데이터를 엔터티화하지만 XML 지시문은 그렇지 않습니다. 요소에는 <Summary> 쿼리에 XML <Summary>This is summary description</Summary>이 할당됩니다.

다음 쿼리를 고려합니다.

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

이것이 결과입니다. 요약 설명이 결과에 엔터티화되어 있습니다.

<ProductModel ProdModelID="19" Name="Mountain-100">
  <Summary>
    <SummaryDescription><Summary>This is summary description</Summary></SummaryDescription>
  </Summary>
</ProductModel>

이제 열 이름에 XML 지시문을 지정하면 ELEMENT 지시문 대신 엔터티화 없이 요약 설명을 받게 됩니다. Summary!2!SummaryDescription!XML

<ProductModel ProdModelID="19" Name="Mountain-100">
  <Summary>
    <SummaryDescription>
      <Summary>This is summary description</Summary>
    </SummaryDescription>
  </Summary>
</ProductModel>

정적 XML 값을 할당하는 대신 다음 쿼리는 xml 형식의 메서드를 사용하여 query() xml 형식의 CatalogDescription 열에서 제품 모델 요약 설명을 검색합니다. 결과는 xml 형식으로 알려져 있으므로 엔터티화가 적용되지 않습니다.

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="http://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;

참고 항목