조건 식(XQuery)

적용 대상:SQL Server

XQuery는 다음 조건 부 if-then-else 문을 지원합니다.

if (<expression1>)  
then  
  <expression2>  
else  
  <expression3>  

expression1의 유효한 부울 값에 따라 expression2expression3이 계산됩니다. 예:

  • 테스트 식 expression1이 빈 시퀀스를 생성하면 결과는 False입니다.

  • 테스트 식 expression1이 단순한 부울 값이 될 경우 이 값은 식의 결과입니다.

  • 테스트 식 expression1이 하나 이상의 노드 시퀀스를 생성하면 식의 결과는 True입니다.

  • 위의 경우 중 하나에 해당하지 않으면 정적 오류가 발생합니다.

또한 다음 사항에 유의하세요.

  • 테스트 식은 괄호 사이에 묶어야 합니다.

  • 다른 식이 필요합니다. 필요하지 않은 경우 이 항목의 예제에 설명된 대로 "() "를 반환할 수 있습니다.

예를 들어 다음 쿼리는 xml 형식 변수에 대해 지정됩니다. if 조건은 sql:variable() 함수 확장 함수를 사용하여 XQuery 식 내의 SQL 변수(@v) 값을 테스트합니다. 변수 값이 "FirstName"이면 요소를 반환합니다 <FirstName> . 그렇지 않으면 요소를 반환합니다 <LastName> .

declare @x xml  
declare @v varchar(20)  
set @v='FirstName'  
set @x='  
<ROOT rootID="2">  
  <FirstName>fname</FirstName>  
  <LastName>lname</LastName>  
</ROOT>'  
SELECT @x.query('  
if ( sql:variable("@v")="FirstName" ) then  
  /ROOT/FirstName  
 else  
   /ROOT/LastName  
')  

결과는 다음과 같습니다.

<FirstName>fname</FirstName>  

다음 쿼리는 특정 제품 모델의 제품 카탈로그 설명에서 처음 두 개의 기능 설명을 검색합니다. 문서에 더 많은 기능이 있는 경우 빈 콘텐츠가 있는 <there-is-more> 요소를 추가합니다.

SELECT CatalogDescription.query('  
     declare namespace p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
     <Product>   
          { /p1:ProductDescription/@ProductModelID }  
          { /p1:ProductDescription/@ProductModelName }   
          {  
            for $f in /p1:ProductDescription/p1:Features/*[position()\<=2]  
            return  
            $f   
          }  
          {  
            if (count(/p1:ProductDescription/p1:Features/*) > 2)  
            then \<there-is-more/>  
            else ()  
          }   
     </Product>          
') as x  
FROM Production.ProductModel  
WHERE ProductModelID = 19  

이전 쿼리에서 if 식의 조건은 두 개 이상의 자식 요소가 있는지 여부를 확인합니다><Features. 그렇다면 결과의 요소를 반환 \<there-is-more/> 합니다.

결과는 다음과 같습니다.

<Product ProductModelID="19" ProductModelName="Mountain 100">  
  \<p1:Warranty xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">  
    \<p1:WarrantyPeriod>3 years\</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 years\</p2:NoOfYears>  
    \<p2:Description>maintenance contract available through your dealer or any AdventureWorks retail store.\</p2:Description>  
  \</p2:Maintenance>  
  \<there-is-more />  
</Product>  

다음 쿼리 <Location> 에서는 작업 센터 위치에서 설치 시간을 지정하지 않으면 LocationID 특성이 있는 요소가 반환됩니다.

SELECT Instructions.query('  
     declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";  
        for $WC in //AWMI:root/AWMI:Location  
        return  
        if ( $WC[not(@SetupHours)] )  
        then  
          <WorkCenterLocation>  
             { $WC/@LocationID }   
          </WorkCenterLocation>  
         else  
           ()  
') as Result  
FROM Production.ProductModel  
where ProductModelID=7  

결과는 다음과 같습니다.

<WorkCenterLocation LocationID="30" />  
<WorkCenterLocation LocationID="45" />  
<WorkCenterLocation LocationID="60" />  

다음 예제와 같이 if 절 없이 이 쿼리를 작성할 수 있습니다.

SELECT Instructions.query('  
     declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";  
for $WC in //AWMI:root/AWMI:Location[not(@SetupHours)]   
        return  
          <Location>  
             { $WC/@LocationID }   
          </Location>  
') as Result  
FROM Production.ProductModel  
where ProductModelID=7  

참고 항목

XQuery 식