query() 메서드(xml 데이터 형식)

적용 대상:SQL ServerAzure SQL DatabaseAzure SQL Managed Instance

xml 데이터 형식의 인스턴스에 대해 XQuery를 지정합니다. 결과는 xml 형식입니다. 이 메서드는 형식화되지 않은 XML의 인스턴스를 반환합니다.

Syntax

query ('XQuery')  

참고 항목

SQL Server 2014(12.x) 및 이전 버전에 대한 Transact-SQL 구문을 보려면 이전 버전 설명서를 참조 하세요.

인수

XQuery
XML 인스턴스에서 요소 및 특성과 같은 XML 노드를 쿼리하는 문자열인 XQuery 식입니다.

예제

이 섹션에서는 xml 데이터 형식의 query() 메서드를 사용하는 예제를 제공합니다.

A. xml 형식 변수에 대해 query() 메서드 사용

다음 예에서는 xml 형식의 @myDoc 변수를 선언하고 여기에 XML 인스턴스를 할당합니다. 그런 다음, query() 메서드를 사용하여 문서에 대해 XQuery를 지정합니다.

이 쿼리는 <ProductDescription> 요소의 <Features> 자식 요소를 검색합니다.

DECLARE @myDoc XML  
SET @myDoc = '<Root>  
<ProductDescription ProductID="1" ProductName="Road Bike">  
<Features>  
  <Warranty>1 year parts and labor</Warranty>  
  <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>  
</Features>  
</ProductDescription>  
</Root>'  
SELECT @myDoc.query('/Root/ProductDescription/Features')  

다음 출력은 결과를 보여줍니다.

<Features>  
  <Warranty>1 year parts and labor</Warranty>  
  <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>  
</Features>        

B. XML 형식 열에 대해 query() 메서드 사용

다음 예제에서 query() 메서드는 AdventureWorks 데이터베이스에서 xml 형식의 CatalogDescription 열에 대해 XQuery를 지정하는 데 사용됩니다.

SELECT CatalogDescription.query('  
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
<Product ProductModelID="{ /PD:ProductDescription[1]/@ProductModelID }" />  
') as Result  
FROM Production.ProductModel  
where CatalogDescription.exist('  
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
declare namespace wm="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain";  
     /PD:ProductDescription/PD:Features/wm:Warranty ') = 1  

이전 쿼리에서 다음 항목을 확인합니다.

  • CatalogDescription 열은 형식화된 xml 열이므로 연결된 스키마 컬렉션이 있습니다. XQuery 프롤로그에서 네임스페이스 키워드(keyword) 나중에 쿼리 본문에 사용되는 접두사를 정의합니다.

  • query() 메서드는 ProductModelID 특성이 있는 XML <Product> 요소를 생성하는데, 이 경우 ProductModelID 특성 값을 데이터베이스에서 검색합니다. XML 생성에 대한 자세한 내용은 XML 생성(XQuery)을 참조하세요.

  • WHERE 절의 exist() 메서드(XML 데이터 형식)는 XML에서 <Warranty> 요소를 포함하는 행만 찾습니다. 네임스페이 키워드(keyword) 두 개의 네임스페이스 접두사를 정의합니다.

다음 출력은 부분 결과를 보여줍니다.

<Product ProductModelID="19"/>   
<Product ProductModelID="23"/>   
...  

query() 메서드와 exist() 메서드는 모두 PD 접두사를 선언합니다. 이 경우 WITH XMLNAMESPACES를 사용하여 접두사를 먼저 정의하고 쿼리에서 이를 사용할 수 있습니다.

WITH XMLNAMESPACES 
(  
   'https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription' AS PD,  
   'https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain' AS WM
)  
SELECT CatalogDescription.query('<Product ProductModelID="{ /PD:ProductDescription[1]/@ProductModelID }" />')
       AS Result  
FROM Production.ProductModel  
WHERE CatalogDescription.exist('/PD:ProductDescription/PD:Features/WM:Warranty ') = 1;

참고 항목

WITH XMLNAMESPACES를 사용하여 쿼리에 네임스페이스 추가
형식화된 XML과 형식화되지 않은 XML 비교
XML 데이터의 인스턴스 만들기
xml 데이터 형식 메서드
XML DML(XML 데이터 수정 언어)