Share via


SequenceType deyimler (XQuery)

XQuery içinde her zaman bir değerdir.Değer türü, bir sıra türü olarak adlandırılır.Sıra türü kullanılabilir bir instance of XQuery ifade. XQuery belirtiminde tanımlanan SequenceType sözdizimi kullanılan bir türünde bir XQuery başvurmak istediğinizde ifade.

Atomik tür adı da kullanılabilir cast as XQuery ifade. Içinde SQL Server, instance of ve cast as SequenceTypes XQuery ifadelerini kısmen desteklenir.

işleç örnek

The instance of işleç can be used to determine the dynamic, or run-saat, type of the value of the specified ifade. Örneğin:

Expression instance of SequenceType[Occurrence indicator]

Dikkat instance of işleç, Occurrence indicator, önem, maddelerin elde edilen sıra numarasını belirtir. Bu seçenek belirtilmezse önem 1 olduğu varsayılır.Içinde SQL Server, soru işareti yalnızca ()?) oluşumu göstergesi desteklenir. The ? occurrence indicator indicates that Expression can return zero or one öğe. If the ? occurrence indicator is specified, instance of returns True when the Expression type matches the specified SequenceType, regardless of whether Expression returns a singleton or an empty sequence.

If the ? occurrence indicator is not specified, sequence of returns True only when the Expression type matches the Type specified and Expression returns a singleton.

Not Artı simgesi (+) ve yıldız işareti (*) oluşumu göstergeleri içinde desteklenmez. SQL Server.

Aşağıdaki örnekler kullanımını göstermektedirinstance of XQuery işleç.

Örnek A

Aşağıdaki örnek oluşturur bir xml değişken yazın ve bunu karşı sorgu belirtir. Sorgu ifadesi belirtir bir instance of ilk işlenen tarafından döndürülen değeri dinamik türü için ikinci işlenen belirtilen tür eşleşip eşleşmediğini belirlemek için işleç'ı tıklatın.

125 Değeri belirtilen türünde bir örnek olduğu için true, aşağıdaki sorgu verir... xs:integer:

declare @x xml
set @x=''
select @x.query('125 instance of xs:integer')
go

Değer ifade tarafından döndürülen aşağıdaki sorgu doğru sayı /a [1], birinci işleneni bir öğe, çünkü:

declare @x xml
set @x='<a>1</a>'
select @x.query('/a[1] instance of element()')
go

Benzer şekilde, instance of değer türü ifade ilk deyimin öznitelik olduğundan, aşağıdaki sorguda, true verir:

declare @x xml
set @x='<a attr1="x">1</a>'
select @x.query('/a[1]/@attr1 instance of attribute()')
go

Aşağıdaki örnekte, ifade, data(/a[1], xdt:untypedAtomic yazılan atomik bir değer döndürür. Bu nedenle, instance of True verir.

declare @x xml
set @x='<a>1</a>'
select @x.query('data(/a[1]) instance of xdt:untypedAtomic')
go

Ifade, aşağıdaki sorguda data(/a[1]/@attrA, türlenmemiş bir atomik değerini verir. Bu nedenle, instance of True verir.

declare @x xml
set @x='<a attrA="X">1</a>'
select @x.query('data(/a[1]/@attrA) instance of xdt:untypedAtomic')
go

Örnek: B

Bu örnek, AdventureWorks örnek veritabanındaki yazılan bir XML sütun sorguladığınız.Sorgulanan sütun ile ilişkili XML şema koleksiyon yazım bilgileri sağlar.

Içinde ifade, data() yazılı xs:dize sütun ile ilişkili şemaya göre türü olan Productmodelıd özniteliği değerini verir. Bu nedenle, instance of True verir.

SELECT CatalogDescription.query('
   declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
   data(/PD:ProductDescription[1]/@ProductModelID) instance of xs:string
') as Result
FROM Production.ProductModel
WHERE ProductModelID = 19

Daha fazla bilgi için bkz:Klavyeyle yazılan XML Untyped XML'YE karşılaştırılması.XML şema koleksiyon CatalogDescription sütun ile ilişkili hakkında daha fazla bilgi için bkz: Sütun ProductModel.CatalogDescription xml hakkında.

The following queries usetheBoole instance of ifade to determine whether the LocationID öznitelik is of xs:integer type:

SELECT Instructions.query('
   declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
   /AWMI:root[1]/AWMI:Location[1]/@LocationID instance of attribute(LocationID,xs:integer)
') as Result
FROM Production.ProductModel
WHERE ProductModelID=7

Aşağıdaki sorgu karşı CatalogDescription belirtilen yazılı XML sütun.Bu sütun ile ilişkili XML şema koleksiyon yazım bilgi sağlar.XML şema koleksiyon hakkında daha fazla bilgi için bkz: Sütun ProductModel.CatalogDescription xml hakkında.

Sorguyu kullanan element(ElementName, ElementType?) sınamanızı instance of ifade doğrulamak için /PD:ProductDescription[1] bir öğe düğümü, belirli bir ad ile türünü verir.

SELECT CatalogDescription.query('
     declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
     /PD:ProductDescription[1] instance of element(PD:ProductDescription, PD:ProductDescription?)
    ') as Result
FROM  Production.ProductModel
where ProductModelID=19

Sorgu için doğru'yu döndürür.

Örneğin, C

Birleşim türleri kullanırken instance of ifade SQL Server bir sınırlama vardır: Özellikle, bir öğe veya öznitelik türü bir birleşim türü iseinstance of tam türü belirlenemedi. Sonuç olarak, bir sorgu SequenceType içinde kullanılan bir atomik türleri gerçek tür bir ifade simpleType sıradüzeninde en üst değilse, false, döndürecektir.Diğer bir deyişle SequenceType içinde belirtilen bir atomik türleri anySimpleType doğrudan alt olmalıdır.Tür hiyerarşisini hakkında daha fazla bilgi için bkz: XQuery'da türü çevrim kuralları.

Sonraki sorgu örneği aşağıdakileri gerçekleştirir:

  • Birleşim türü, bir tamsayı olarak veya dize türü, içinde tanımlı olan bir XML şema koleksiyon oluştur.

  • Yazılı bir bildirin. xml XML şema koleksiyon kullanarak deðiþken.

  • Bir örnek XML örneği değişkenine atayın.

  • Değişken göstermek için sorgu instance of bir birleşim türü ile ilgili olduğunda davranışı.

Bu sorguyu oluşur:

CREATE XML SCHEMA COLLECTION MyTestSchema AS '
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ns" xmlns:ns="http://ns">
<simpleType name="MyUnionType">
<union memberTypes="integer string"/>
</simpleType>
<element name="TestElement" type="ns:MyUnionType"/>
</schema>'
Go

Aşağıdaki sorgu, False verir, bu SequenceType belirtilen çünkü instance of ifade, en yüksek ana gerçek belirtilen ifade türü değil. Değeri, <TestElement> bir tamsayı türüdür. En üst xs:decimal ' dir.Ancak, bunu olarak için ikinci işlenen belirtilmemiş instance of işleç.

SET QUOTED_IDENTIFIER ON
DECLARE @var XML(MyTestSchema)

SET @var = '<TestElement xmlns="http://ns">123</TestElement>'

SELECT @var.query('declare namespace ns="http://ns" 
   data(/ns:TestElement[1]) instance of xs:integer')
go

En üst xs:integer xs:decimal olduğundan, sorguyu değiştirin ve sorgu SequenceType xs:decimal belirtmek için sorgu doğru döndürecektir.

SET QUOTED_IDENTIFIER ON
DECLARE @var XML(MyTestSchema)
SET @var = '<TestElement xmlns="http://ns">123</TestElement>'
SELECT @var.query('declare namespace ns="http://ns"   
   data(/ns:TestElement[1]) instance of xs:decimal')
go

Örnek: B

Bu örnekte, ilk önce bir XML şema koleksiyon oluşturur ve yazmak için kullanmak bir xml değişken. Yazılan xml değişken göstermek için daha sonra sorgulanan instance of işlevi.

Bir basit tür myType ve öğe, aşağıdaki XML şema koleksiyon tanımlar <root>, türü myType:

drop xml schema collection SC
go
CREATE XML SCHEMA COLLECTION SC AS '
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="myNS" xmlns:ns="myNS"
xmlns:s="https://schemas.microsoft.com/sqlserver/2004/sqltypes">
      <import namespace="https://schemas.microsoft.com/sqlserver/2004/sqltypes"/>
      <simpleType name="myType">
           <restriction base="s:varchar">
                  <maxLength value="20"/>
            </restriction>
      </simpleType>
      <element name="root" type="ns:myType"/>
</schema>'
Go

Şimdi bir yazılı oluşturma xml değişken ve bu sorgu:

DECLARE @var XML(SC)
SET @var = '<root xmlns="myNS">My data</root>'
SELECT @var.query('declare namespace sqltypes = "https://schemas.microsoft.com/sqlserver/2004/sqltypes";
declare namespace ns="myNS"; 
   data(/ns:root[1]) instance of ns:myType')
go

Çünkü, varchar türünden sqltypes şemada tanımlanan kısıtlamaya göre myType türü türetir. instance of Ayrıca, true döndürecektir.

DECLARE @var XML(SC)
SET @var = '<root xmlns="myNS">My data</root>'
SELECT @var.query('declare namespace sqltypes = "https://schemas.microsoft.com/sqlserver/2004/sqltypes";
declare namespace ns="myNS"; 
data(/ns:root[1]) instance of sqltypes:varchar?')
go

Örnek E

Aşağıdaki örnekte, ifade IDREFS öznitelik değerlerinden birini alır ve kullanır instance of değer türü IDREF olup olmadığını belirlemek için . Örneğin, aşağıdakileri gerçekleştirir:

  • Bir XML şema koleksiyon içinde oluşturur <Customer> öğenin bir OrderList IDREFS türü öznitelik, ve var olan bir <Order> öğe Sipariş Kimliği ID türü öznitelik.

  • Yazılı bir oluşturur. XML değişken ve XML örneği için örnek atar.

  • Sorguda karşı değişkeni belirtir.Sorgu ifade ilk sipariş KIMLIĞI değeri ilk OrderList IDRERS türü özniteliğinden alır. <Customer>. Alınan değeri IDREF türüdür.Bu nedenle, instance of True verir.

create xml schema collection SC as
'<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:Customers="Customers" targetNamespace="Customers">
            <element name="Customers" type="Customers:CustomersType"/>
            <complexType name="CustomersType">
                        <sequence>
                            <element name="Customer" type="Customers:CustomerType" minOccurs="0" maxOccurs="unbounded" />
                        </sequence>
            </complexType>
             <complexType name="OrderType">
                <sequence minOccurs="0" maxOccurs="unbounded">
                            <choice>
                                <element name="OrderValue" type="integer" minOccurs="0" maxOccurs="unbounded"/>
                            </choice>
                </sequence>                                           
                <attribute name="OrderID" type="ID" />
            </complexType>

            <complexType name="CustomerType">
                <sequence minOccurs="0" maxOccurs="unbounded">
                            <choice>
                                <element name="spouse" type="string" minOccurs="0" maxOccurs="unbounded"/>
                                <element name="Order" type="Customers:OrderType" minOccurs="0" maxOccurs="unbounded"/>
                            </choice>
                </sequence>                                           
                <attribute name="CustomerID" type="string" />
                <attribute name="OrderList" type="IDREFS" />
            </complexType>
 </schema>'
go
declare @x xml(SC)
set @x='<CustOrders:Customers xmlns:CustOrders="Customers">
                <Customer CustomerID="C1" OrderList="OrderA OrderB"  >
                              <spouse>Jenny</spouse>
                                <Order OrderID="OrderA"><OrderValue>11</OrderValue></Order>
                                <Order OrderID="OrderB"><OrderValue>22</OrderValue></Order>

                </Customer>
                <Customer CustomerID="C2" OrderList="OrderC OrderD" >
                                <spouse>John</spouse>
                                <Order OrderID="OrderC"><OrderValue>33</OrderValue></Order>
                                <Order OrderID="OrderD"><OrderValue>44</OrderValue></Order>

                        </Customer>
                <Customer CustomerID="C3"  OrderList="OrderE OrderF" >
                                <spouse>Jane</spouse>
                                <Order OrderID="OrderE"><OrderValue>55</OrderValue></Order>
                                <Order OrderID="OrderF"><OrderValue>55</OrderValue></Order>
                </Customer>
                <Customer CustomerID="C4"  OrderList="OrderG"  >
                                <spouse>Tim</spouse>
                                <Order OrderID="OrderG"><OrderValue>66</OrderValue></Order>
                        </Customer>
                <Customer CustomerID="C5"  >
                </Customer>
                <Customer CustomerID="C6" >
                </Customer>
                <Customer CustomerID="C7"  >
                </Customer>
</CustOrders:Customers>'

select @x.query(' declare namespace CustOrders="Customers"; 
 data(CustOrders:Customers/Customer[1]/@OrderList)[1] instance of xs:IDREF ? ') as XML_result

Uygulama kısıtlamaları

Sınırlamaları şunlardır:

  • The schema-element() and schema-attribute() sequence types are not supported for comparison to the instance of işleç.

  • Tam sıraları, örneğin, (1,2) instance of xs:integer*, desteklenmiyor.

  • Bir formu kullandığınızda element() bir tür adı gibi belirten sıra türü element(ElementName, TypeName), bir soru işareti (?) türü belirtilmiş. For example, element(Title, xs:string?) indicates that the element might be null.SQL Server does not support run-time detection of the xsi:nil property by using instance of.

  • Değeri Expression öğe veya öznitelik bir bileşim yazılan gelir SQL Server , yalnızca temel öğe, yok türetilmiş, tanımlamak, değer türü, türetilmiş gelen yazın. Örneğin, <e1> statik bir türü olarak tanımlanır (xs:integer | xs:dize), aşağıdaki false döndürür.

    data(<e1>123</e1>) instance of xs:integer
    

    Ancak, data(<e1>123</e1>) instance of xs:decimal True döndürecektir.

  • Için processing-instruction() ve document-node() sıra türü, yalnızca bağımsız değişken olmadan formlar izin verilir. Örneğin, processing-instruction() izin verilir, ancak processing-instruction('abc') izin verilmiyor.

işleç atama

The cast as ifade can be used to convert a value to a specific data type. Örneğin:

Expression cast as  AtomicType?

Içinde SQL Server, soru işareti (?) sonra gereklidir AtomicType. Örneğin, aşağıdaki sorguda, gösterilen "2" cast as xs:integer? dize değeri tamsayıya dönüştürür:

declare @x xml
set @x=''
select @x.query('"2" cast as xs:integer?')

Aşağıdaki sorguda data() klavyeyle yazılan dize türü Productmodelıd özniteliğinin değerini verir. The cast asişleç converts the value to xs:integer.

WITH XMLNAMESPACES ('https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription' AS PD)
SELECT CatalogDescription.query('
   data(/PD:ProductDescription[1]/@ProductModelID) cast as xs:integer?
') as Result
FROM Production.ProductModel
WHERE ProductModelID = 19

Açık kullanımı data() Bu sorguda gerekli değildir. The cast as ifade performs implicit atomization on the input ifade.

Oluşturucu işlevler

Atomik tür Oluşturucu işlevleri kullanabilirsiniz.Örneğin, yerine cast as işleç "2" cast as xs:integer?, kullanabileceğiniz xs:integer() kurucu fonksiyonunu aşağıdaki örnekte olduğu gibi:

declare @x xml
set @x=''
select @x.query('xs:integer("2")')

Aşağıdaki örnek, 2000-01-01Z eşit bir xs:tarih değeri döndürür.

declare @x xml
set @x=''
select @x.query('xs:date("2000-01-01Z")')

Oluşturucular kullanıcı tanımlı atomik türleri için de kullanabilirsiniz.Örneğin, ilişkili XML şema koleksiyon, XML veri türü bir basit tür tanımlar bir myType() kurucu türü değeri döndürmek için kullanılır.

Uygulama kısıtlamaları

  • XQuery ifadeler typeswitch, castable, ve treat desteklenmez.

  • cast as soru işareti (?) gerektirir.sonra atomik türü.

  • xs:QName Çevrim için bir türü desteklenmiyor.Kullanımı expanded-QName Bunun yerine.

  • xs:date, xs:time, ve xs:datetime Z belirtilir, saat dilimi, gerektirir.

    Saat dilimini belirtilmedi için aşağıdaki sorgu başarısız olur.

    DECLARE @var XML
    SET @var = ''
    SELECT @var.query(' <a>{xs:date("2002-05-25")}</a>')
    go
    

    Z saat dilimi göstergesi değeri eklediğinizde, sorgu çalışır.

    DECLARE @var XML
    SET @var = ''
    SELECT @var.query(' <a>{xs:date("2002-05-25Z")}</a>')
    go
    

    Bu sonucu oluşur:

    <a>2002-05-25Z</a>