계층 포함 XQuery

AdventureWorks 데이터베이스에 있는 대부분의 xml 유형의 열은 반구조화된 문서입니다. 따라서 각 행에 저장된 문서는 다르게 보일 수 있습니다. 이 항목의 쿼리 예제에서는 이러한 여러 문서로부터 정보를 추출하는 방법을 보여 줍니다.

1. 제조 지침 문서에서 해당 위치의 첫 번째 제조 단계와 함께 작업 센터 위치 검색

제품 모델 7의 경우 쿼리는 ProductModelIDProductModelName 특성이 포함된 <ManuInstr> 요소와 하나 이상의 <Location> 자식 요소가 있는 XML을 생성합니다.

각 <Location> 요소에는 자체 특성 집합과 하나의 <step> 자식 요소가 있습니다. 이 <step> 자식 요소는 작업 센터 위치의 첫 번째 제조 단계입니다.

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

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

  • XQuery 프롤로그namespace 키워드는 네임스페이스 접두사를 정의합니다. 이 접두사는 나중에 쿼리 본문에서 사용됩니다.
  • 컨텍스트 변환 토큰인 {)와 (}는 XML 생성의 쿼리를 쿼리 평가로 변환하는 데 사용됩니다.
  • **sql:column()**은 생성 중인 XML의 관계형 값을 포함하는 데 사용됩니다.
  • <Location> 요소를 생성할 때 $wc/@*는 모든 작업 센터 위치 특성을 검색합니다.
  • string() 함수는 <step> 요소로부터 문자열 값을 반환합니다.

다음은 결과의 일부입니다.

<ManuInstr ProdModelID="7" ProductModelName="HL Touring Frame">
   <Location LocationID="10" SetupHours="0.5" 
            MachineHours="3" LaborHours="2.5" LotSize="100">
     <step1>Insert aluminum sheet MS-2341 into the T-85A 
             framing tool.</step1>
   </Location>
   <Location LocationID="20" SetupHours="0.15" 
            MachineHours="2" LaborHours="1.75" LotSize="1">
      <step1>Assemble all frame components following 
             blueprint 1299.</step1>
   </Location>
...
</ManuInstr> 

2. AdditionalContactInfo 열에서 모든 전화 번호 찾기

다음 쿼리는 <telephoneNumber> 요소에 대한 전체 계층을 검색하여 특정 고객 연락처의 추가 전화 번호를 검색합니다. <telephoneNumber> 요소는 계층의 아무 곳에나 표시될 수 있기 때문에 이 쿼리는 검색 시 하위 항목과 자체 연산자(//)를 사용합니다.

SELECT AdditionalContactInfo.query('
 declare namespace ci="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactInfo";
 declare namespace act="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes";
for $ph in /ci:AdditionalContactInfo//act:telephoneNumber
   return
      $ph/act:number
') as x
FROM  Person.Contact
WHERE ContactID = 1

다음은 결과입니다.

<act:number 
  xmlns:act="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes">
  111-111-1111
</act:number>
<act:number 
  xmlns:act="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes">
  112-111-1111
</act:number>

<AdditionalContactInfo>의 <telephoneNumber> 자식 요소와 같이 최상위 전화 번호만 검색하려면 쿼리의 FOR 식이 다음으로 변경됩니다.

for $ph in /ci:AdditionalContactInfo/act:telephoneNumber

참고 항목

개념

XQuery 기초
XML 생성(XQuery)
xml 데이터 형식

도움말 및 정보

SQL Server 2005 지원 받기