Share via


順序に関係する XQuery

適用対象:SQL Server

リレーショナル データベースにはシーケンスの概念はありません。 たとえば、"データベースから最初の顧客を取得する" などの要求を行うことはできません。ただし、XML ドキュメントに対してクエリを実行し、最初 <の Customer> 要素を取得できます。 その後、常に同じ顧客を取得します。

このトピックでは、ドキュメントにノードが表示されるシーケンスに基づくクエリについて説明します。

A. 製品の 2 番目の作業センターの場所で製造手順を取得する

次のクエリでは、特定の製品モデルに対して、製造プロセス内にあるワーク センターの場所の順序で 2 番目のワーク センターの場所で製造手順が取得されます。

SELECT Instructions.query('  
     declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";  
    <ManuStep ProdModelID = "{sql:column("Production.ProductModel.ProductModelID")}"  
                ProductModelName = "{ sql:column("Production.ProductModel.Name") }" >  
     <Location>  
       { (//AWMI:root/AWMI:Location)[2]/@* }  
       <Steps>  
         { for $s in (//AWMI:root/AWMI:Location)[2]//AWMI:step  
           return  
              <Step>  
               { string($s) }  
              </Step>  
         }  
        </Steps>  
      </Location>  
     </ManuStep>  
') as Result  
FROM Production.ProductModel  
WHERE ProductModelID=7  

上のクエリに関して、次の点に注意してください。

  • 中かっこ内の式は、評価の結果に置き換えられます。 詳細については、「 XML 構築 (XQuery)」を参照してください。

  • @* は、2 番目の作業センターの場所のすべての属性を取得します。

  • FLWOR イテレーション (FOR ...RETURN) は、2 番目の <step> 作業センターの場所のすべての子要素を取得します。

  • sql:column() 関数 (XQuery) には、構築中の XML にリレーショナル値が含まれています。

結果を次に示します。

<ManuStep ProdModelID="7" ProductModelName="HL Touring Frame">  
  <Location LocationID="20" SetupHours="0.15"   
              MachineHours="2"  LaborHours="1.75" LotSize="1">  
  <Steps>  
   <Step>Assemble all frame components following blueprint 1299.</Step>  
     ...  
  </Steps>  
 </Location>  
</ManuStep>    

前のクエリでは、テキスト ノードのみを取得します。 代わりに要素全体 <step> を返す場合は、クエリから string() 関数を削除します。

B. 製品の製造における 2 番目のワーク センターの場所で使用されるすべての材料とツールを検索する

次のクエリでは、特定の製品モデルに対して、製造プロセス内にあるワーク センターの場所の順序で 2 番目のワーク センターの場所で使用されるツールと材料が取得されます。

SELECT Instructions.query('  
    declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";  
   <Location>  
      { (//AWMI:root/AWMI:Location)[1]/@* }  
       <Tools>  
         { for $s in (//AWMI:root/AWMI:Location)[1]//AWMI:step//AWMI:tool  
           return  
              <Tool>  
                { string($s) }  
              </Tool>  
          }  
        </Tools>  
        <Materials>  
            { for $s in (//AWMI:root/AWMI:Location)[1]//AWMI:step//AWMI:material  
              return  
                 <Material>  
                    { string($s) }  
                 </Material>  
             }  
         </Materials>  
  </Location>  
') as Result  
FROM Production.ProductModel  
where ProductModelID=7  

上のクエリに関して、次の点に注意してください。

  • クエリは Location> 要素を<構築し、データベースからその属性値を取得します。

  • 2 つの FLWOR (for...return) イテレーション: 1 つはツールを取得し、1 つは使用されるマテリアルを取得します。

結果を次に示します。

<Location LocationID="10" SetupHours=".5"   
          MachineHours="3" LaborHours="2.5" LotSize="100">  
  <Tools>  
    <Tool>T-85A framing tool</Tool>  
    <Tool>Trim Jig TJ-26</Tool>  
    <Tool>router with a carbide tip 15</Tool>  
    <Tool>Forming Tool FT-15</Tool>  
  </Tools>  
  <Materials>  
    <Material>aluminum sheet MS-2341</Material>  
  </Materials>  
</Location>  

C. 製品カタログから最初の 2 つの製品機能の説明を取得する

特定の製品モデルの場合、クエリは製品モデル カタログの 要素から最初の <Features> 2 つの特徴の説明を取得します。

SELECT CatalogDescription.query('  
     declare namespace p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
     <ProductModel ProductModelID= "{ data( (/p1:ProductDescription/@ProductModelID)[1] ) }"  
                   ProductModelName = "{ data( (/p1:ProductDescription/@ProductModelName)[1] ) }" >  
       {  
         for $F in /p1:ProductDescription/p1:Features  
         return   
           $F/*[position() <= 2]   
       }  
     </ProductModel>  
      ') as x  
FROM Production.ProductModel  
where ProductModelID=19  

上のクエリに関して、次の点に注意してください。

クエリ本文は、ProductModelID 属性と ProductModelName 属性を持つ 要素を含む <ProductModel> XML を構築します。

  • クエリでは FOR ... を使用します。RETURN ループを使用して、製品モデルの機能の説明を取得します。 position() 関数は、最初の 2 つの特徴を取得するために使用されます。

結果を次に示します。

<ProductModel ProductModelID="19" ProductModelName="Mountain 100">  
 <p1:Warranty   
  xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">  
  <p1:WarrantyPeriod>3 year</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</p2:NoOfYears>  
  <p2:Description>maintenance contact available through your dealer   
            or any AdventureWorks retail store.  
  </p2:Description>  
 </p2:Maintenance>  
</ProductModel>   

D. 製品の製造プロセスの最初の作業センターの場所で使用される最初の 2 つのツールを見つける

製品モデルの場合、このクエリは、製造プロセス内の一連のワーク センターの場所の最初の作業センターの場所で使用される最初の 2 つのツールを返します。 クエリは、Production.ProductModel テーブルの Instructions 列に格納されている製造指示に対して指定されます。

SELECT Instructions.query('  
     declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";  
   for $Inst in (//AWMI:root/AWMI:Location)[1]  
   return   
     <Location>  
       { $Inst/@* }  
       <Tools>  
         { for $s in ($Inst//AWMI:step//AWMI:tool)[position() <= 2]  
           return  
             <Tool>  
               { string($s) }  
             </Tool>  
         }  
       </Tools>  
     </Location>  
') as Result  
FROM Production.ProductModel  
where ProductModelID=7  

結果を次に示します。

<Location LocationID="10" SetupHours=".5"   
            MachineHours="3" LaborHours="2.5" LotSize="100">  
  <Tools>  
    <Tool>T-85A framing tool</Tool>  
    <Tool>Trim Jig TJ-26</Tool>  
  </Tools>  
</Location>   

E. 特定の製品を製造する際の最初のワーク センターの場所で最後の 2 つの製造手順の検索

クエリでは last() 関数を使用して、最後の 2 つの製造手順を取得します。

SELECT 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  

結果を次に示します。

<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 データ (SQL Server)
XQuery 言語リファレンス (SQL Server)
XML の構築 (XQuery)