Share via


シーケンスの関数 - id

適用対象:SQL Server

$argで指定された 1 つ以上の xs:IDREF 値の値と一致する xs:ID 値を持つ要素ノードのシーケンス 返します。

構文

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

引数

$arg
1 つ以上の xs:IDREF 値。

解説

関数の結果は、XML インスタンス内の要素のシーケンスであり、ドキュメントの順序で、候補 xs:IDREFs の一覧にある xs:IDREFs の 1 つ以上に等しい xs:ID 値を持ちます。

xs:IDREF 値がどの要素とも一致しない場合は、空のシーケンスを返します。

このトピックでは、データベース内のさまざまな xml 型の列に格納されている XML インスタンスに対する XQuery の例を AdventureWorks2022 示します。

A. IDREF 属性値に基づく要素の取得

次の例では、fn:id を使用して、IDREF マネージャー属性に基づいて要素を取得 <employee> します。 この例では、マネージャー属性は IDREF 型の属性で、eid 属性は ID 型の属性です。

特定のマネージャー属性値の場合、 id() 関数は、ID 型属性値が入力 IDREF 値と一致する要素を検索 <employee> します。 つまり、特定の従業員に対して、 id() 関数は従業員マネージャーを返します。

この例では次のことが行われます。

  • XML スキーマ コレクションが作成されます。

  • 型指定された xml 変数は、XML スキーマ コレクションを使用して作成されます。

  • クエリは、 要素の マネージャー IDREF 属性によって参照される ID 属性値を持つ要素を <employee> 取得します。

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

このクエリでは、値として "Dave" が返されます。 これは、Dave が Joe のマネージャーであることを示します。

B. OrderList IDREFS 属性値に基づいて要素を取得する

次の例では、要素の <Customer> OrderList 属性は IDREFS 型の属性です。 この例では特定の顧客に対応する注文 ID がリストされます。 注文 ID ごとに、 の下に注文値を<>Order><Customer指定する要素の子があります。

クエリ式 data(CustOrders:Customers/Customer[1]/@OrderList)[1] では、最初の顧客の最初の値が IDRES 一覧から取得されます。 この値は id () 関数に渡されます。 次に、OrderID 属性値が <Order>id() 関数への入力と一致する要素を検索します。

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>  

実装の制限事項

制限事項は次のとおりです。

  • SQL Serverでは、id() の 2 引数バージョンはサポートされていません。

  • SQL Serverには、引数 id() の型が xs:IDREF* のサブタイプである必要があります。

参照

シーケンスの関数