條件運算式 (XQuery)

XQuery 支援下列 if-then-else 條件陳述式:

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

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

  • 如果測試運算式 expression1 產生空白時序,結果即為 False。

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

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

  • 否則,就會產生靜態錯誤。

同時應注意下列項目:

  • 測試運算式必須在括號內。

  • else 運算式為必要的。如果您不需要它,您可以傳回 " ( ) ",如本主題中的範例所說明。

例如,下列查詢是根據 xml 類型變數所指定。if 條件可使用 sql:variable() function 延伸模組函數,來測試 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

在上述查詢中,在 if 運算式中的條件,會檢查在 <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

請參閱

概念