title | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic | helpviewer_keywords | dev_langs | |||
---|---|---|---|---|---|---|---|---|---|---|---|---|
empty Function (XQuery) |
Learn about the XQuery function empty() that returns a value indicating whether a specified sequence of items is empty. |
rothja |
jroth |
03/09/2017 |
sql |
xml |
language-reference |
|
|
[!INCLUDE SQL Server Azure SQL Database]
Returns True if the value of $arg is an empty sequence. Otherwise, the function returns False.
fn:empty($arg as item()*) as xs:boolean
$arg
A sequence of items. If the sequence is empty, the function returns True. Otherwise, the function returns False.
The fn:exists() function is not supported. As an alternative, the not() function can be used.
This topic provides XQuery examples against XML instances that are stored in various xml type columns in the AdventureWorks database.
In the manufacturing process for Product Model 7, this query returns all the work center locations that do not have a MachineHours attribute.
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
This is the result:
ProductModelID Result
-------------- ------------------------------------------
7 <Location LocationID="30" LaborHrs="1"/>
<Location LocationID="50" LaborHrs="3"/>
<Location LocationID="60" LaborHrs="4"/>
The following, slightly modified, query returns "NotFound" if the MachineHour attribute is not present:
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
This is the result:
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"/>
XQuery Functions against the xml Data Type
exist() Method (xml Data Type)