Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
$arg'de sağlanan xs:IDREF değerlerinden birinin veya birkaçının değerleriyle eşleşen xs:ID değerlerine sahip element düğümlerinin dizisini döndürür.
Sözdizimi
fn:id($arg as xs:IDREF*) as element()*
Arguments
$arg
Bir veya daha fazla xs:IDREF değeri.
Açıklamalar
Fonksiyonun sonucu, XML örneğinde, belge sırasıyla bir veya daha fazla xs:ID değerine sahip bir dizi öğe dizisi olur; bu değerin aday xs:IDREF listesinde bir veya daha fazla xs:IDREF'e eşittir.
Eğer xs:IDREF değeri herhangi bir elemanla eşleşmiyorsa, fonksiyon boş diziyi döndürür.
Örnekler
Bu konu, veritabanındaki çeşitli xml türü sütunlarında AdventureWorks2025 depolanan XML örneklerine karşı XQuery örnekleri sağlar.
A. IDREF öznitelik değerine göre elemanların alınması
Aşağıdaki örnek, IDREF yöneticisi özniteliğine göre öğeleri <employee> almak için fn:id kullanır. Bu örnekte, yönetici özniteliği IDREF tipi bir öznitelik, eid özniteliği ise ID tipi özniteliğidir.
Belirli bir yönetici öznitelik değeri için, id() fonksiyonu, ID tipi özellik değeri giriş IDREF değeriyle eşleşen öğeyi bulur <employee> . Başka bir deyişle, belirli bir çalışan için id() fonksiyonu çalışan yöneticisini döndürür.
Örnekte olanlar şöyle:
Bir XML şema koleksiyonu oluşturulur.
Tiplenmiş bir xml değişkeni, XML şema koleksiyonu kullanılarak oluşturulur.
Sorgu, öğenin yöneticisi IDREF özniteliği <
employee> tarafından referans verilen bir ID özellik değerine sahip öğeyi alır.
-- 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
Sorgu, değer olarak "Dave" döner. Bu, Dave'in Joe'nun menajeri olduğunu gösterir.
B. OrderList IDREFS öznitelik değerine dayalı elemanların alınması
Aşağıdaki örnekte, öğenin <Customer> OrderList özniteliği IDREFS tipi bir özniteliktir. O müşterinin sipariş kimliklerini listeliyor. Her order id için, <Order> emir değerini sağlayan bir <Customer> eleman çocuğu vardır.
Sorgu ifadesi, data(CustOrders:Customers/Customer[1]/@OrderList)[1], ilk müşteri için IDRES listesinden ilk değeri alır. Bu değer daha sonra id() fonksiyonuna iletilir. Fonksiyon daha sonra OrderID özellik değeri id() fonksiyonuna girdiye uyan öğeyi bulur<Order>.
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>
Uygulama Sınırlamaları
Sınırlamalar şunlardır:
SQL Server, id()'nin iki argümanlı versiyonunu desteklemez.
SQL Server, id() argüman tipinin xs:IDREF* alt tipi olmasını gerektirir.