次の方法で共有


順序に関係する XQuery

リレーショナル データベースにはシーケンスの概念はありません。たとえば、"データベースから最初の顧客を取得する" などの要求を行うことはできません。ただし、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 ループを使用して、製品モデルの機能説明を取得します。最初の 2 つの機能を取得するために、position() 関数が使用されます。

次に結果を示します。

<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 Adventure Works Cycles 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 つの製造手順の検索

クエリでは、最後の 2 つの製造手順を取得するために、last() 関数が使用されます。

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>