last 函数 (XQuery)

返回当前处理的序列中的项数。 具体来说,它返回序列中最后一项的整数索引。 序列中第一项的索引值为 1。

语法

fn:last() as xs:integer

注释

在 SQL Server 中,fn:last() 只能用于与上下文相关的谓词的上下文中。 特别要指出的是,它只能在方括号 ([ ]) 内使用。

示例

本主题提供了一些针对 XML 实例的 XQuery 示例,这些实例存储在 AdventureWorks 数据库内不同的 xml 类型列中。

A.使用 last() XQuery 函数检索最后两个生产步骤

下面的查询检索某个特定生产模型的最后两个生产步骤。 在此查询中使用 last() 函数返回的值(生产步骤数)来检索最后两个生产步骤。

SELECT ProductModelID, Instructions.query(' 
declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
  <LastTwoManuSteps>
   <Last-1Step> 
     { (/AWMI:root/AWMI:Location)[1]/AWMI:step[(last()-1)]/text() }
   </Last-1Step>
   <LastStep> 
     { (/AWMI:root/AWMI:Location)[1]/AWMI:step[last()]/text() }
   </LastStep>
  </LastTwoManuSteps>
') as Result
FROM Production.ProductModel
WHERE ProductModelID=7

在前面的查询中,//AWMI:root//AWMI:Location)[1]/AWMI:step[last()] 中的 last() 函数返回生产步骤数。 此值用于检索生产车间的最后一个生产步骤。

结果如下:

ProductModelID Result  
-------------- -------------------------------------
7      <LastTwoManuSteps>
         <Last-1Step>
            When finished, inspect the forms for defects per 
            Inspection Specification .
         </Last-1Step>
         <LastStep>Remove the frames from the tool and place them 
            in the Completed or Rejected bin as appropriate.
         </LastStep>
       </LastTwoManuSteps>

请参阅

参考

针对 xml 数据类型的 XQuery 函数