Udostępnij za pośrednictwem


Identyfikator funkcji (XQuery)

Zwraca sekwencja węzłów element o wartości typu xs: ID, które pasują do wartości jednego lub więcej wartości xs:IDREF w $arg.

Składnia

fn:id($arg as xs:IDREF*) as element()*

Argumenty

  • $arg
    Jedna lub więcej wartości xs:IDREF.

Uwagi

Wynik działania funkcja jest sekwencją elementów XML wystąpienie, w kolejności dokumentu, który ma wartość typu xs: ID równa jeden lub więcej xs:IDREFs na liście sugestii xs:IDREFs.

Jeśli wartość xs:IDREF jest niezgodna z dowolnego elementu, funkcja zwraca pusty sekwencji.

Przykłady

W tym temacie przedstawiono przykłady XQuery przeciwko wystąpień XML, które są przechowywane w różnych xml typ kolumn w AdventureWorks2008R2 bazy danych.Przegląd tych kolumn, zobacz XML reprezentacji typu danych w bazie danych AdventureWorks2008R2.

A.Pobieranie elementów na podstawie wartości atrybut IDREF

W poniższym przykładzie użyto fn:id pobrać <employee> elementów, w oparciu o atrybut IDREF menedżera.W tym przykładzie atrybut manager jest atrybut IDREF typu i atrybut eid jest atrybut typu ID.

Określony Menedżer wartości atrybut id() funkcja znajduje <employee> element, którego wartość atrybut ID typu odpowiada wartości wejściowej IDREF.Innymi słowy, dla określonego pracownika id() funkcja zwraca Menedżer pracownika.

Jest to, co się dzieje w przykładzie:

  • Utworzono kolekcja schematu XML.

  • Pismo xml zmienna jest tworzony przy użyciu kolekcja schematu XML.

  • Kwerenda pobiera element, który ma wartość atrybutu ID odwołuje się Menedżera atrybut IDREF <employee> element.

-- If exists, drop the XML schema collection (SC).
-- drop xml schema collection SC
-- go

create xml schema collection SC as
'<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:e="emp" targetNamespace="emp">
            <element name="employees" type="e:EmployeesType"/>
            <complexType name="EmployeesType">
                 <sequence>
                      <element name="employee" type="e:EmployeeType" minOccurs="0" maxOccurs="unbounded" />
                 </sequence>
            </complexType>  
 
            <complexType name="EmployeeType">
                        <attribute name="eid" type="ID" />
                        <attribute name="name" type="string" />
                        <attribute name="manager" type="IDREF" />
            </complexType>       
</schema>'
go

declare @x xml(SC)
set @x='<e:employees xmlns:e="emp">
<employee eid="e1" name="Joe" manager="e10" />
<employee eid="e2" name="Bob" manager="e10" />
<employee eid="e10" name="Dave" manager="e10" />
</e:employees>'
 
select @x.value(' declare namespace e="emp"; 
 (fn:id(e:employees/employee[@name="Joe"]/@manager)/@name)[1]', 'varchar(50)') 
Go

Kwerenda zwraca "Dave" jako wartość.Oznacza to, że Dave jest Menedżer Janusza.

B.Pobieranie elementów na podstawie wartości atrybut OrderList IDREFS

W poniższym przykładzie atrybut OrderList <Customer> element jest atrybut typu IDREFS.Wyświetla listę identyfikatorów zamówienia dla określonego nabywcy.Dla każdego identyfikatora zamówienia jest <Order> element podrzędność pod <Customer> podaje wartość zamówienia.

Wyrażenie kwerendy data(CustOrders:Customers/Customer[1]/@OrderList)[1], pobiera pierwszą wartość z listy IDRES dla pierwszego nabywcy.Wartość ta jest następnie przekazywany do id() funkcja.Funkcja następnie znalezienie <Order> element, którego wartość atrybut IDZamówienia reprezentuje dane wejściowe do id() funkcji.

drop xml schema collection SC
go
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";
  id(data(CustOrders:Customers/Customer[1]/@OrderList)[1])')

-- result
<Order OrderID="OrderA">
  <OrderValue>11</OrderValue>
</Order>

Ograniczenia wdrażania

Są następujące ograniczenia:

  • SQL Servernie obsługuje wersja argumentu dwóch id().

  • SQL Serverwymaga argumentu typu id() się podtyp xs:IDREF*.

Zobacz także

Odwołanie