empty 函數 (XQuery)
如果 $arg 的值是空序列,則傳回 True。 否則,此函數會傳回 False。
語法
fn:empty($arg as item()*) as xs:boolean
引數
- $arg
項目序列。 如果序列是空的,則函數會傳回 True。 否則,此函數會傳回 False。
備註
不支援 fn:exists() 函數。 替代方式是使用 not() 函數。
範例
本主題針對 XML 執行個體提供 XQuery 範例,這些執行個體是儲存在 AdventureWorks 資料庫的各種 xml 類型資料行中。
A.使用 empty() XQuery 函數來決定是否出現屬性
在「產品型號 7」的製造過程中,此查詢傳回所有沒有 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
以下是結果:
ProductModelID Result
-------------- ------------------------------------------
7 <Location LocationID="30" LaborHrs="1"/>
<Location LocationID="50" LaborHrs="3"/>
<Location LocationID="60" LaborHrs="4"/>
下列查詢稍微經過修改,如果 MachineHour 屬性沒有出現的話,它會傳回 "NotFound":
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
以下是結果:
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"/>