Delen via


Voorbeeld: Geef de HIDE-instructie op

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

In dit voorbeeld ziet u het gebruik van de HIDE-instructie . Deze instructie is handig als u wilt dat de query een kenmerk retourneert voor het ordenen van de rijen in de universele tabel die door de query wordt geretourneerd, maar u dat kenmerk niet wilt in het uiteindelijke resulterende XML-document.

Met deze query wordt deze XML samengesteld:

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

Met deze query wordt de gewenste XML gegenereerd. De query identificeert twee kolomgroepen met 1 en 2 als tagwaarden in de kolomnamen.

Deze query maakt gebruik van de methode query() (XML-gegevenstype) van het xml-gegevenstype om een query uit te voeren op de kolom CatalogDescription van het XML-type om de samenvattingsbeschrijving op te halen. De query maakt ook gebruik van de methode value() (XML-gegevenstype) van het XML-gegevenstype om de ProductModelID-waarde op te halen uit de kolom CatalogDescription. Deze waarde is niet vereist in de resulterende XML, maar is vereist om de resulterende rijenset te sorteren. Daarom bevat de kolomnaam [Summary!2!ProductModelID!HIDE], de HIDE-instructie . Als deze kolom niet is opgenomen in de SELECT-instructie, moet u de rijenset sorteren op [ProductModel!1!ProdModelID] en [Summary!2!SummaryDescription], dat van het xml-type is, en kunt u de kolom van het xml-type niet gebruiken in ORDER BY. Daarom wordt de extra [Summary!2!ProductModelID!HIDE] kolom toegevoegd en vervolgens opgegeven in de ORDER BY-component.

USE AdventureWorks2022;
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="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
       (/PD:ProductDescription/@ProductModelID)[1]', 'int'),
        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],[Summary!2!ProductModelID!hide]
FOR XML EXPLICIT;
GO

Dit is het resultaat:

<ProductModel ProdModelID="19" Name="Mountain-100">
  <Summary>
    <SummaryDescription>
      <pd:Summary xmlns:pd="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription" xmlns="">
        <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>

Zie ook