Koşullu İfadeler (XQuery)

Şunlar için geçerlidir: SQL Server

XQuery aşağıdaki koşullu if-then-else ifadesini destekler:

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

Etkin Boolean değerine expression1bağlı olarak, ya expression2 ya da expression3 değerlenir. Örneğin:

  • Test ifadesi, expression1, boş bir dizi çıkarsa, sonuç Yanlıştır.

  • Eğer test ifadesi, expression1, basit bir Boolean değeri verirse, bu değer ifadenin sonucudur.

  • Eğer test ifadesi, expression1, bir veya daha fazla düğüm dizisi oluşturursa, ifadenin sonucu Doğrudur.

  • Aksi takdirde, statik bir hata ortaya çıkar.

Ayrıca aşağıdakilere de dikkat edin:

  • Test ifadesi parantez arasına girmelidir.

  • Else ifadesi gereklidir. Eğer ihtiyacınız yoksa, bu konudaki örneklerde gösterildiği gibi " ( ) " döndürebilirsiniz.

Örneğin, aşağıdaki sorgu xml tip değişkenine karşı belirtilmiştir. If koşulu, XQuery ifadesi içindeki SQL değişkeninin (@v) değerini sql:variable() fonksiyon uzantı fonksiyonu kullanarak test eder. Değişken değeri "FirstName" ise, elemanı <FirstName> döndürür. Aksi takdirde, eleman geri <LastName> döner.

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  
')  

Sonuç şu şekildedir:

<FirstName>fname</FirstName>  

Aşağıdaki sorgu, belirli bir ürün modelinin ürün kataloğu açıklamasından ilk iki özellik açıklamasını alır. Belgede daha fazla özellik varsa, boş içerikli bir <there-is-more> öğe ekler.

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  

Önceki sorguda, if ifadesindeki koşul, içinde <Features>ikiden fazla çocuk eleman olup olmadığını kontrol eder. Eğer evetse, sonuçtaki öğeyi \<there-is-more/> döndürür.

Sonuç şu şekildedir:

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

Aşağıdaki sorguda, çalışma merkezi konumu kurulum saatlerini belirtmiyorsa LocationID özniteliğine sahip bir <Location> eleman döner.

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  

Sonuç şu şekildedir:

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

Bu sorgu, aşağıdaki örnekte gösterildiği gibi if cümlesi olmadan yazılabilir:

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