Share via


条件式 (XQuery)

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>

次のクエリは、特定の製品モデルの製品カタログの説明から、最初の 2 つの製品の特徴の記述を取得します。ドキュメントに複数の特徴が記述されている場合は、内容のない <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> の子要素が 3 つ以上あるかどうかを確認しています。3 つ以上ある場合は、結果に <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

関連項目

概念