Share via


Koşullu deyimler (XQuery)

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

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

Bağlı olarak etkin olan Boole değeri expression1, her iki expression2 veya expression3 değerlendirilir. Örneğin:

  • Varsa sınama ifadenin expression1, sonuçlar boş bir sıradaki sonuçlar yanlış ise.

  • Sınama ifade, expression1, basit bir Boole değeri sonuçlar, bu değer sonucudur ifade.

  • Varsa sınama ifadenin expression1, bir veya daha fazla düğüm, ifade sonucunu bir sıra sonuçlarını true olur.

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

Ayrıca şunları unutmayın:

  • Sınama ifade parantez alınmalıdır.

  • The else ifade is required. Gerek yok, bu konudaki örneklerde gösterildiği gibi "()" geri alabilirsiniz.

Örneğin, aşağıdaki sorgu karşı belirtilen xml türü değişkeni. The if condition tests the value of the SQL variable (@v) inside the XQuery ifade by using the sql:variable() işlev extension işlev.Değişken değeri "FirstName" ise, sayı <FirstName> öğe. Aksi halde fonksiyonu <LastName> öğe.

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

Bu sonucu oluşur:

<FirstName>fname</FirstName>

Aşağıdaki sorguyu ilk iki özellik açıklamaları, belirli bir ürün modeline ürün kataloğunu açıklamasından alır.Varsa diğer özellikler belgede, ekler bir <there-is-more> öğe boş içeriğe sahip.

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

Koşulda önceki sorguda if ifade, ikiden fazla alt öğe olup olmadığını denetler. <Features>. Evet, döndürür <there-is-more/> Sonuç öğe.

Bu sonucu oluşur:

<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 sorgudaki bir <Location> iş merkezi konumu Kur saat belirtirseniz, LocationID özniteliği olan öğesi döndürülür.

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

Bu sonucu oluşur:

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

Bu sorgu olmadan yazılabilir if Aşağıdaki örnekte gösterildiği gibi yan:

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

See Also

Concepts