Funktionen für Sequenzen – empty

Gilt für:SQL Server

Gibt True zurück, wenn der Wert von $arg eine leere Sequenz ist. Andernfalls gibt die Funktion False zurück.

Syntax

  
fn:empty($arg as item()*) as xs:boolean  

Argumente

$arg
Eine Sequenz aus Elementen. Falls die Sequenz leer ist, gibt die Funktion True zurück. Andernfalls gibt die Funktion False zurück.

Bemerkungen

Die fn:exists() -Funktion wird nicht unterstützt. Alternativ kann die Funktion not() verwendet werden.

Beispiele

Dieses Thema enthält XQuery-Beispiele für XML-Instanzen, die in verschiedenen Xml-Typspalten in der AdventureWorks-Datenbank gespeichert sind.

A. Verwenden der empty() XQuery-Funktion zum Bestimmen, ob ein Attribut vorhanden ist

Im Herstellungsprozess für Produktmodell 7 gibt diese Abfrage alle Arbeitscenterstandorte zurück, die kein MachineHours-Attribut aufweisen.

SELECT ProductModelID, Instructions.query('  
declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";  
     for $i in /AWMI:root/AWMI:Location[empty(@MachineHours)]  
     return  
       <Location  
            LocationID="{ ($i/@LocationID) }"  
            LaborHrs="{ ($i/@LaborHours) }" >  
            {   
              $i/@MachineHours  
            }    
       </Location>  
') as Result  
FROM Production.ProductModel  
where ProductModelID=7  

Dies ist das Ergebnis:

ProductModelID      Result          
-------------- ------------------------------------------  
7              <Location LocationID="30" LaborHrs="1"/>  
               <Location LocationID="50" LaborHrs="3"/>  
               <Location LocationID="60" LaborHrs="4"/>  

Die folgende, leicht geänderte Abfrage gibt "NotFound" zurück, wenn das MachineHour-Attribut nicht vorhanden ist:

SELECT ProductModelID, Instructions.query('  
declare namespace p14="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";  
     for $i in /p14:root/p14:Location  
     return  
       <Location  
            LocationID="{ ($i/@LocationID) }"  
            LaborHrs="{ ($i/@LaborHours) }" >  
            {   
                 if (empty($i/@MachineHours)) then  
                    attribute MachineHours { "NotFound" }  
                 else  
                    attribute MachineHours { data($i/@MachineHours) }  
            }    
       </Location>  
') as Result  
FROM Production.ProductModel  
where ProductModelID=7  

Dies ist das Ergebnis:

ProductModelID Result                         
-------------- -----------------------------------  
7                
  <Location LocationID="10" LaborHrs="2.5" MachineHours="3"/>  
  <Location LocationID="20" LaborHrs="1.75" MachineHours="2"/>  
  <Location LocationID="30" LaborHrs="1" MachineHours="NotFound"/>  
  <Location LocationID="45" LaborHrs="0.5" MachineHours="0.65"/>  
  <Location LocationID="50" LaborHrs="3" MachineHours="NotFound"/>  
  <Location LocationID="60" LaborHrs="4" MachineHours="NotFound"/>  

Weitere Informationen

XQuery-Funktionen für den xml-Datentyp
exist()-Methode (XML-Datentyp)