基于序列的函数 - empty

适用于:SQL Server

如果 $arg 的值为空序列,则返回 True。 否则,该函数返回 False。

语法

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

参数

$arg
项序列。 如果该序列为空,则此函数返回 True。 否则,该函数返回 False。

备注

不支持 fn:exists () 函数。 作为替代方法,可以使用 not () 函数。

示例

本主题提供针对存储在 AdventureWorks 数据库中各种 xml 类型列中的 XML 实例的 XQuery 示例。

A. 使用 empty() XQuery 函数来确定属性是否存在

在 Product Model 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"/>  

另请参阅

针对 xml 数据类型的 XQuery 函数
exist() 方法(xml 数据类型)