分享方式:


條件運算式 (XQuery)

適用於:SQL Server

XQuery 支援下列條件 式 if-then-else 語句:

if (<expression1>)  
then  
  <expression2>  
else  
  <expression3>  

視 的有效布林值 expression1 而定, expression2 評估 或 expression3 。 例如:

  • 如果測試運算式 expression1 , 會產生空序列,則結果為 False。

  • 如果測試運算式 expression1 會產生簡單的布林值,這個值就是運算式的結果。

  • 如果測試運算式 expression1 、 會產生一或多個節點的序列,則運算式的結果為 True。

  • 否則,會引發靜態錯誤。

另請注意下列事項:

  • 測試運算式必須括在括弧之間。

  • 需要 else 運算式。 如果您不需要它,您可以傳回 「( ) 」,如本主題中的範例所示。

例如,針對 xml 類型變數指定 下列查詢。 if 條件會使用 sql:variable() 函式擴充函數,測試 XQuery 運算式內 SQL 變數 (@v) 的值。 如果變數值為 「FirstName」,則會傳 <FirstName> 回 元素。 否則,它會傳 <LastName> 回 專案。

declare @x xml  
declare @v varchar(20)  
set @v='FirstName'  
set @x='  
<ROOT rootID="2">  
  <FirstName>fname</FirstName>  
  <LastName>lname</LastName>  
</ROOT>'  
SELECT @x.query('  
if ( sql:variable("@v")="FirstName" ) then  
  /ROOT/FirstName  
 else  
   /ROOT/LastName  
')  

以下是結果:

<FirstName>fname</FirstName>  

下列查詢會從特定產品模型的產品目錄描述擷取前兩個功能描述。 如果檔中有更多功能,它會新增 <there-is-more> 具有空白內容的元素。

SELECT CatalogDescription.query('  
     declare namespace p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
     <Product>   
          { /p1:ProductDescription/@ProductModelID }  
          { /p1:ProductDescription/@ProductModelName }   
          {  
            for $f in /p1:ProductDescription/p1:Features/*[position()\<=2]  
            return  
            $f   
          }  
          {  
            if (count(/p1:ProductDescription/p1:Features/*) > 2)  
            then \<there-is-more/>  
            else ()  
          }   
     </Product>          
') as x  
FROM Production.ProductModel  
WHERE ProductModelID = 19  

在上一個查詢中,運算式中的 條件會檢查 中的 <Features> 兩個以上的子專案。 如果是,則會傳 \<there-is-more/> 回結果中的 專案。

以下是結果:

<Product ProductModelID="19" ProductModelName="Mountain 100">  
  \<p1:Warranty xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">  
    \<p1:WarrantyPeriod>3 years\</p1:WarrantyPeriod>  
    \<p1:Description>parts and labor\</p1:Description>  
  \</p1:Warranty>  
  \<p2:Maintenance xmlns:p2="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">  
    \<p2:NoOfYears>10 years\</p2:NoOfYears>  
    \<p2:Description>maintenance contract available through your dealer or any AdventureWorks retail store.\</p2:Description>  
  \</p2:Maintenance>  
  \<there-is-more />  
</Product>  

在下列查詢中,如果工作中心位置未指定安裝時數, <Location> 則會傳回具有 LocationID 屬性的專案。

SELECT Instructions.query('  
     declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";  
        for $WC in //AWMI:root/AWMI:Location  
        return  
        if ( $WC[not(@SetupHours)] )  
        then  
          <WorkCenterLocation>  
             { $WC/@LocationID }   
          </WorkCenterLocation>  
         else  
           ()  
') as Result  
FROM Production.ProductModel  
where ProductModelID=7  

以下是結果:

<WorkCenterLocation LocationID="30" />  
<WorkCenterLocation LocationID="45" />  
<WorkCenterLocation LocationID="60" />  

此查詢可以在沒有 if 子句的情況下 撰寫,如下列範例所示:

SELECT Instructions.query('  
     declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";  
for $WC in //AWMI:root/AWMI:Location[not(@SetupHours)]   
        return  
          <Location>  
             { $WC/@LocationID }   
          </Location>  
') as Result  
FROM Production.ProductModel  
where ProductModelID=7  

另請參閱

XQuery 運算式