Aracılığıyla paylaş


Genel XQuery kullan durumlar

Bu konuda genel XQuery örnekler kullanmak sağlar.

Örnekler

A.Katalog açıklamaları ürünleri ve ağırlıklarını bulmak için sorgu

Aşağıdaki sorgu ürün modeli kimlikleri verir ve ağırlıkları, ürün katalog açıklaması varsa.Sorguyu aşağıdaki biçime sahiptir xml oluşturur:

<Product ProductModelID="…">
  <Weight>…</Weight>
</Product>

Bu sorgu şöyledir:

SELECT CatalogDescription.query('
declare namespace p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
  <Product  ProductModelID="{ (/p1:ProductDescription/@ProductModelID)[1] }">
     { 
       /p1:ProductDescription/p1:Specifications/Weight 
     } 
  </Product>
') as Result
FROM Production.ProductModel
WHERE CatalogDescription is not null

Önceki sorgudan aşağıdakilere dikkat edin:

  • The namespace keyword in the XQuery prolog defines a namespace prefix that is used in the query body.

  • Gerekli xml sorgu gövde yapıları.

  • where yan tümce tümce tümcesinde, exist() yöntem yalnızca ürün katalog açıklamaları içeren satırları bulmak için kullanılır.Diğer bir deyişle içeren xml <ProductDescription> öğesi.

Bu sonucu verir:

<Product ProductModelID="19"/>
<Product ProductModelID="23"/> 
<Product ProductModelID="25"/> 
<Product ProductModelID="28"><Weight>Varies with size.</Weight></Product>
<Product ProductModelID="34"/>
<Product ProductModelID="35"/>

Aynı bilgileri aşağıdaki sorgu alır, ancak yalnızca bu ürün modelleri için Ağırlık katalog açıklaması içerir <Weight> öğesinde belirtimleri, <Specifications> öğesi.Bu örnek ile xmlnamespaces pd önek ve ad alanını bildirirsiniz kullanır bağlama.Bu yolla bağlama her ikisini de açıklanmayan query() yöntem ve exist() yöntem.

WITH XMLNAMESPACES ('https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription' AS pd)
SELECT CatalogDescription.query('
          <Product  ProductModelID="{ (/pd:ProductDescription/@ProductModelID)[1] }">
                 { 
                      /pd:ProductDescription/pd:Specifications/Weight 
                 } 
          </Product>
') as x
FROM Production.ProductModel
WHERE CatalogDescription.exist('/pd:ProductDescription/pd:Specifications//Weight ') = 1

Önceki sorgu, exist() yöntem xml veri türü olup olmadığını görmek için where yan tümce tümce tümce denetler bir <Weight> öğesinde <Specifications> öğesi.

B.Ürün modeli kimlikleri boyutu ön açı ve küçük resimler, katalog açıklamaları içeren ürün modelleri için Bul

Ürün resimleri, ürün katalog xml açıklama içeren <Picture> öğesi.Her resmin birçok özelliğe sahiptir.Bunlar resmi açı <Angle> öğesi ve boyutu <Size> öğesi.

Ön açı ve küçük boyutlu resimler, katalog açıklamaları içeren ürün modelleri için sorgu aşağıdaki biçime sahiptir xml oluşturur:

< Product ProductModelID="…">
  <Picture>
    <Angle>front</Angle>
    <Size>small</Size>
  </Picture>
</Product>
WITH XMLNAMESPACES ('https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription' AS pd)
SELECT CatalogDescription.query('
   <pd:Product  ProductModelID="{ (/pd:ProductDescription/@ProductModelID)[1] }">
      <Picture>
         {  /pd:ProductDescription/pd:Picture/pd:Angle } 
         {  /pd:ProductDescription/pd:Picture/pd:Size } 
      </Picture>
   </pd:Product>
') as Result
FROM  Production.ProductModel
WHERE CatalogDescription.exist('/pd:ProductDescription/pd:Picture') = 1
AND   CatalogDescription.value('(/pd:ProductDescription/pd:Picture/pd:Angle)[1]', 'varchar(20)')  = 'front'
AND   CatalogDescription.value('(/pd:ProductDescription/pd:Picture/pd:Size)[1]', 'varchar(20)')  = 'small'

Önceki sorgudan aşağıdakilere dikkat edin:

  • where yan tümce tümce tümcesinde, exist() yöntem ile birlikte Ürün Katalog açıklamaları olan satırları almak için kullanılan <Picture> öğesi.

  • where yan tümce tümce tümce kullanan value() değerleri karşılaştırmak için yöntem iki kez <Size> ve <Angle> öğeleri.

Bu kısmi bir sonucudur:

<p1:Product 
  xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription" 
  ProductModelID="19">
  <Picture>
    <p1:Angle>front</p1:Angle>
    <p1:Size>small</p1:Size>
  </Picture>
</p1:Product>
...

C.İçine her çifti ile model adı ve özellik çiftlerini ürün düz bir listesini oluşturmak <Özellikler> öğesi

Ürün modeli katalog açıklamasını xml birkaç ürün özellikleri içerir.Tüm bu özellikler dahil <Features> öğesi.Sorguyu kullanır (XQuery) xml yapım gerekli xml oluşturmak için.Küme parantezleri içinde ifade sonucu değiştirilir.

SELECT CatalogDescription.query('
declare namespace p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
  for $pd in /p1:ProductDescription,
   $f in $pd/p1:Features/*
  return
   <Feature>
     <ProductModelName> { data($pd/@ProductModelName) } </ProductModelName>
     { $f }
  </Feature>        
') as x
FROM Production.ProductModel
WHERE ProductModelID=19

Önceki sorgudan aşağıdakilere dikkat edin:

  • $pd / p1:Features / * düğümü alt öğesi verir <Features>, ancak $pd/p1:Features/node() verir tüm düğümler.Bu öğe düğümleri, metin düğümleri, iþleme yönergeleri ve açıklamaları içerir.

  • İki döngüler için hangi ürün adı ve tek tek özellik döndürülen bir Kartezyen ürün oluşturur.

  • The ProductName is an attribute.Bu sorgu xml yapım bir öğesi olarak geri döndürür.

Bu kısmi bir sonucudur:

<Feature>
 <ProductModelName>Mountain 100</ProductModelName>
 <ProductModelID>19</ProductModelID>
 <p1:Warranty 
   xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">
    <p1:WarrantyPeriod>3 year</p1:WarrantyPeriod>
    <p1:Description>parts and labor</p1:Description>
 </p1:Warranty>
</Feature>
<Feature>
 <ProductModelName>Mountain 100</ProductModelName>
 <ProductModelID>19</ProductModelID>
 <p2:Maintenance xmlns:p2="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">
    <p2:NoOfYears>10</p2:NoOfYears>
    <p2:Description>maintenance contact available through your dealer 
           or any Adventure Works retail store.</p2:Description>
    </p2:Maintenance>
</Feature>
...
...    

D.Bir ürün modeli katalog açıklamasından listesi ürün modeli kimliği adı, model ve gruplandırılmış özellikleri içinde bir <Ürün> öğesi

Ürün modeli katalog açıklamasını depolanan bilgileri kullanarak, aşağıdaki sorgu ürün modeli adı, modeli kimliği, listeler ve gruplandırılmış özellikleri içinde bir <Ürün> öğesi.

SELECT ProductModelID, CatalogDescription.query('
     declare namespace pd="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
     <Product>
         <ProductModelName> 
           { data(/pd:ProductDescription/@ProductModelName) } 
         </ProductModelName>
         <ProductModelID> 
           { data(/pd:ProductDescription/@ProductModelID) } 
         </ProductModelID>
         { /pd:ProductDescription/pd:Features/* }
     </Product>        
') as x
FROM Production.ProductModel
WHERE ProductModelID=19

Bu kısmi bir sonucudur:

<Product>
  <ProductModelName>Mountain 100</ProductModelName>
  <ProductModelID>19</ProductModelID>
  <p1:Warranty>... </p1:Warranty>
  <p2:Maintenance>...  </p2:Maintenance>
  <p3:wheel xmlns:p3="https://www.adventure-works.com/schemas/OtherFeatures">High performance wheels.</p3:wheel>
  <p4:saddle xmlns:p4="https://www.adventure-works.com/schemas/OtherFeatures">
    <p5:i xmlns:p5="http://www.w3.org/1999/xhtml">Anatomic design</p5:i> and made from durable leather for a full-day of riding in comfort.</p4:saddle>
  <p6:pedal xmlns:p6="https://www.adventure-works.com/schemas/OtherFeatures">
    <p7:b xmlns:p7="http://www.w3.org/1999/xhtml">Top-of-the-line</p7:b> clipless pedals with adjustable tension.</p6:pedal>
   ...

E.Ürün modeli özellik açıklamaları almak

Aşağıdaki sorgu içeren bir xml yapıları bir <Product> öğesi olan ProducModelID, ProductModelName öznitelikleri ve ilk iki ürün özellikleri.Özellikle ilk iki alt öğelerini ilk iki ürün özellikleri olan <Features> öğesi.Daha fazla özellik yoksa, boş döndürür <There-is-more/> öğesi.

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

Önceki sorgudan aşağıdakilere dikkat edin:

  • İÇİN...Dönüş döngü yapısı ilk iki ürün özelliklerini alır.The position() function is used to find the position of the elements in the sequence.

F.Ürün Katalog açıklamasından "ons" ile biten öğe adları Bul

Aşağıdaki sorgu, katalog açıklamaları arar ve bulunan tüm öğeleri döndürür <ProductDescription> öğesi adı "ons" ile biter.

SELECT ProductModelID, CatalogDescription.query('
     declare namespace p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
      for $pd in /p1:ProductDescription/*[substring(local-name(.),string-length(local-name(.))-2,3)="ons"]
      return 
          <Root>
             { $pd }
          </Root>
') as Result
FROM Production.ProductModel
WHERE CatalogDescription is not NULL

Bu kısmi bir sonucudur:

ProductModelID   Result
-----------------------------------------
         19        <Root>       
                     <p1:Specifications xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">        
                          ...       
                     </p1:Specifications>       
                   </Root>        

G."Aerodynamic" sözcüğünü içeren Özet açıklamaları bulma

Aşağıdaki sorgu Ürün modelleri, katalog açıklamaları "Aerodynamic" sözcüğü yer alır. Özet Açıklama:

WITH XMLNAMESPACES ('https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription' AS pd)
SELECT ProductModelID, CatalogDescription.query('
          <Prod >
             { /pd:ProductDescription/@ProductModelID }
             { /pd:ProductDescription/pd:Summary }
          </Prod>
 ') as Result
FROM Production.ProductModel
WHERE CatalogDescription.value('
     contains( string( (/pd:ProductDescription/pd:Summary)[1] ),"Aerodynamic")','bit') = 1

SEÇME sorgusu belirtir bir Not query() ve value() yöntemleri xml veri türü.Bu nedenle, ad alanları yinelenen yerine bildirim iki kez iki fark sorgu prologs, önek pd sorguda kullanılan ve birlikte xmlnamespaces kullanarak yalnızca bir kez tanımlanır.

Önceki sorgudan aşağıdakilere dikkat edin:

  • where yan tümce tümce tümce yalnızca burada katalog açıklaması "Aerodynamic" sözcüğünü içeren satırları almak için kullanılan de <Summary> öğesi.

  • The contains() function is used to see if the word is included in the text.

  • The value() method of the xml data type compares the value returned by contains() to 1.

Bu sonucu verir:

ProductModelID Result      
-------------- ------------------------------------------
28     <Prod ProductModelID="28">
        <pd:Summary xmlns:pd="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">
       <p1:p xmlns:p1="http://www.w3.org/1999/xhtml">
         A TRUE multi-sport bike that offers streamlined riding and a
         revolutionary design. Aerodynamic design lets you ride with the 
         pros, and the gearing will conquer hilly roads.</p1:p>
       </pd:Summary>
      </Prod>  

H.Ürün modelleri, katalog açıklamaları ürün modeli resimleri eklemek Bul

Aşağıdaki sorgu alır Ürün Katalog açıklamaları yapıyor, Hayır modeller için ProductModelIDs dahil bir <Picture> öğesi.

SELECT  ProductModelID
FROM    Production.ProductModel
WHERE   CatalogDescription is not NULL
AND     CatalogDescription.exist('declare namespace p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
     /p1:ProductDescription/p1:Picture
') = 0

Önceki sorgudan aşağıdakilere dikkat edin:

  • If the exist() method in the WHERE clause returns False (0), the product model ID is returned.Aksi halde, onu döndürülmez.

  • Ürün açıklamaları içerdiğinden bir <Picture> öğesi, sonuç küme içinde bu boş durum.