id 함수(XQuery)
$arg에 제공된 xs:IDREF 값 중 하나 이상의 값에 일치하는 xs:ID 값을 가진 요소 노드의 시퀀스를 반환합니다.
구문
fn:id($arg as xs:IDREF*) as element()*
인수
- $arg
하나 이상의 xs:IDREF 값입니다.
주의
함수 결과는 후보 xs:IDREF 목록에 있는 하나 이상의 xs:IDREF에 일치하는 xs:ID 값이 있는 문서 순서대로 된 XML 인스턴스의 요소 시퀀스입니다.
요소와 일치하는 xs:IDREF 값이 없으면 이 함수는 빈 시퀀스를 반환합니다.
예
이 항목에서는 AdventureWorks2012 데이터베이스의 다양한 xml 유형 열에 저장된 XML 인스턴스에 대한 XQuery 예를 제공합니다.
1.IDREF 특성 값을 기반으로 요소 검색
다음 예에서는 fn:id를 사용하여 IDREF 관리자 특성을 기반으로 <employee> 요소를 검색합니다. 이 예에서 관리자 특성은 IDREF 유형 특성이고 eid 특성은 ID 유형 특성입니다.
특정 관리자 특성 값의 경우 id() 함수는 ID 유형 특성 값이 입력 IDREF 값과 일치하는 <employee> 요소를 찾습니다. 즉, 특정 직원에 대해 id() 함수는 직원 관리자를 반환합니다.
다음은 이 예에서 수행된 작업입니다.
XML 스키마 컬렉션이 생성됩니다.
형식화된 xml 변수는 XML 스키마 컬렉션을 사용하여 생성됩니다.
쿼리는 <employee> 요소의 manager IDREF 특성에서 참조한 ID 특성 값이 있는 요소를 검색합니다.
-- 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의 관리자라는 의미입니다.
2.OrderList IDREFS 특성 값을 기반으로 요소 검색
다음 예에서 <Customer> 요소의 OrderList 특성은 IDREFS 유형 특성입니다. 특정 고객에 대한 주문 ID를 나열합니다. 각 주문 ID의 경우 주문 값을 제공하는 <Customer> 아래에 <Order> 요소 자식이 있습니다.
쿼리 식 data(CustOrders:Customers/Customer[1]/@OrderList)[1]은 첫 번째 고객에 대한 IDRES 목록에서 첫 번째 값을 검색합니다. 그러면 이 값이 id() 함수에 전달됩니다. 그런 다음 이 함수는 OrderID 특성 값이 id() 함수에 대한 입력과 일치하는 <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>
구현 시 제한 사항
제한 사항은 다음과 같습니다.
SQL Server는 인수가 두 개인 id() 버전을 지원하지 않습니다.
SQL Server에는 xs:IDREF*의 하위 유형이 되는 id() 인수 유형이 필요합니다.