分享方式:


CoNtext Functions - last (XQuery)

適用於:SQL Server

傳回目前正在處理之序列中的專案數目。 具體而言,它會傳回序列中最後一個專案的整數索引。 序列中的第一個專案索引值為 1。

語法

  
fn:last() as xs:integer  

備註

在 SQL Server 中, fn:last() 只能在內容相依述詞的內容中使用。 具體來說,它只能用在括弧內( [ ] )。

範例

本主題針對 AdventureWorks 資料庫中各種 xml 類型資料行中儲存的 XML 實例,提供 XQuery 範例。

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 函式