共用方式為


與順序有關的 XQuery

適用於:SQL Server

關係資料庫沒有循序的概念。 例如,您無法提出「從資料庫取得第一個客戶」之類的要求。不過,您可以查詢 XML 檔並擷取第一個 < Customer > 元素。 然後,您一律會擷取相同的客戶。

本主題說明根據節點出現在檔中的順序進行查詢。

範例

A. 擷取產品第二個工作中心位置的製造步驟

針對特定產品模型,下列查詢會擷取製造步驟,位於製造程式中工作中心位置序列中的第二個工作中心位置。

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)

  • @* 擷取第二個工作中心位置的所有屬性。

  • FLWOR 反復專案 (FOR ...RETURN) 會擷取第二個工作中心位置的所有 <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. 尋找產品製造中第二個工作中心位置使用的所有材料和工具

針對特定產品模型,下列查詢會擷取製造程式中工作中心位置序列中第二個工作中心位置所使用的工具和材質。

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  

請注意下列項目是從上一個查詢而來:

  • 查詢會 < 建構 Loca tion> 元素,並從資料庫擷取其屬性值。

  • 它使用兩個 FLWOR (for...return) 反復專案:一個用來擷取工具,另一個用來擷取所使用的材質。

以下是結果:

<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. 從產品目錄擷取前兩個產品功能描述

針對特定產品模型,查詢會從 <Features> 產品模型目錄中的 元素擷取前兩個功能描述。

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  

請注意下列項目是從上一個查詢而來:

查詢主體會建構 XML,其中包含 <ProductModel> 具有 ProductModelID 和 ProductModelName 屬性的專案。

  • 查詢會使用 FOR ...RETURN 迴圈可擷取產品模型功能描述。 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 AdventureWorks retail store.  
  </p2:Description>  
 </p2:Maintenance>  
</ProductModel>   

D. 尋找產品製造過程中第一個工作中心位置所使用的前兩個工具

針對產品模型,此查詢會傳回製造過程中工作中心位置序列中第一個工作中心位置所使用的前兩個工具。 查詢會根據 Production.ProductModel 資料表的 [指令 ] 資料行中所 儲存的 製造指令來指定。

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. 在製造特定產品的第一個工作中心位置尋找最後兩個製造步驟

查詢會使用 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>  

另請參閱

XML 資料 (SQL Server)
Xquery 語言參考 (SQL Server)
XML 建構 (XQuery)