Share via


Funzione empty (XQuery)

Restituisce True se il valore di $arg è una sequenza vuota. In caso contrario, restituisce False.

Sintassi

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

Argomenti

  • $arg
    Una sequenza di elementi. Se la sequenza è vuota, la funzione restituisce True. In caso contrario, restituisce False.

Osservazioni

La funzione fn:exists() non è supportata. In alternativa, è possibile utilizzare la funzione not().

Esempi

In questo argomento vengono forniti esempi di utilizzo del linguaggio XQuery sulle istanze XML archiviate in diverse colonne di tipo xml nel database AdventureWorks. Per una panoramica su ognuna di queste colonne, vedere Rappresentazione del tipo di dati XML nel database AdventureWorks.

A. Utilizzo della funzione XQuery empty() per determinare se un attributo è presente

Nel processo di produzione del modello Product Model 7, questa query restituisce tutti i centri di lavorazione per i quali non è presente l'attributo MachineHours.

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

Risultato:

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

La query seguente è simile alla precedente e restituisce "NotFound" se l'attributo MachineHour non è presente:

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

Risultato:

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"/>

Vedere anche

Riferimento

Funzioni XQuery per il tipo di dati XML
Metodo exist() (tipo di dati xml)

Guida in linea e informazioni

Assistenza su SQL Server 2005