Voorbeeld: Geef de ELEMENT-instructie en entiteitscodering op

van toepassing op:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceSQL-database in Microsoft Fabric

In dit voorbeeld ziet u het verschil tussen de ELEMENT- en XML--instructies. De ELEMENT-instructie entitiseert de gegevens, maar de XML-instructie doet dat niet. Het element <Summary> is toegewezen aan XML, <Summary>This is summary description</Summary>, in de query.

Note

In Azure SQL Database en SQL Database in Microsoft Fabric zijn de USE instructie niet beschikbaar. Maak in plaats daarvan direct verbinding met de beoogde database en voer de rest van het codevoorbeeld uit.

Houd rekening met deze query:

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;

Dit is het resultaat. De samenvattingsbeschrijving wordt in het resultaat verwerkt.

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

Als u nu de XML--instructie opgeeft in de kolomnaam, Summary!2!SummaryDescription!XML, in plaats van de ELEMENT-instructie, ontvangt u de samenvattingsbeschrijving zonder entitisatie.

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

In plaats van een statische XML-waarde toe te wijzen, gebruikt de volgende query de query() methode van het xml--type om de beschrijving van het productmodeloverzicht op te halen uit de kolom CatalogDescription van xml- type. Omdat het resultaat bekend is van xml- type, wordt er geen entitisatie toegepast.

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;