정렬 포함 XQuery

관계형 데이터베이스에는 시퀀스 개념이 없습니다. 예를 들어 "데이터베이스에서 첫 번째 고객 가져오기"와 같은 요청은 수행할 수 없습니다. 하지만 XML 문서를 쿼리하면 첫 번째 <Customer> 요소를 검색할 수 있습니다. 그런 다음에는 항상 같은 고객을 검색할 수 있습니다.

이 항목에서는 문서에 노드가 표시되는 시퀀스에 기반한 쿼리에 대해 설명합니다.

1. 두 번째 작업 센터 위치에서 제품에 대한 제조 단계 검색

특정 제품 모델에 대해 다음 쿼리는 두 번째 작업 센터 위치에서 제조 프로세스에 있는 작업 센터 위치의 시퀀스에 따라 제조 단계를 검색합니다.

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() 함수를 제거합니다.

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> 요소를 생성하고 데이터베이스에서 해당 특성 값을 검색합니다.
  • 여기에서는 두 개의 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>

3. 제품 카탈로그에서 처음 두 개의 제품 기능 설명 검색

특정 제품 모델에 대해 다음 쿼리는 제품 모델 카탈로그에 있는 <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

이전 쿼리에서 다음을 유의하십시오.

이 쿼리 본문에서는 ProductModelID 및 ProductModelName 특성이 있는 <ProductModel> 요소가 포함된 XML을 생성합니다.

  • 이 쿼리에서는 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> 

4. 제품의 제조 프로세스에서 첫 번째 작업 센터 위치에 사용되는 처음 두 개의 도구 검색

제품 모델에 대해 다음 쿼리는 제조 프로세스의 작업 센터 위치 시퀀스에 따라 첫 번째 작업 센터 위치에서 사용되는 처음 두 개의 도구를 반환합니다. 이 쿼리는 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> 

5. 특정 제품의 제조 시 첫 번째 작업 센터 위치에서 마지막 두 개의 제조 단계 검색

다음 쿼리에서는 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 데이터 형식
XML 생성(XQuery)

관련 자료

xml 데이터 형식에 대한 XQuery

도움말 및 정보

SQL Server 2005 지원 받기