Share via


계층 포함 XQuery

적용 대상:SQL Server

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

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

제품 모델 7의 경우 쿼리는 ProductModelID 및 ProductModelName 특성과 하나 이상의Location<> 자식 요소를 사용하여 요소를 포함하는ManuInstr><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 프롤로그네임스페이스 키워드는 네임스페이스 접두사를 정의합니다. 이 접두사는 쿼리 본문의 뒷부분에서 사용됩니다.

  • 컨텍스트 전환 토큰 {) 및 (}은(는) 쿼리를 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>   

B. 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>  

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

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

참고 항목

XQuery 기본 사항
XML 생성(XQuery)
XML 데이터(SQL Server)