T-SQL OPENXML[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 centver 的 blog


示例
a.使用带有 openxml 的 select 语句。

下面的示例使用 sp_xml_preparedocument 创建 xml 图像的内部表示。然后对 xml 文档的内部表示法执行使用 openxml 行集提供程序的 select 语句。

flag 值设置为 1,表示以特性为中心的映射。因此,xml 特性映射到行集中的列。指定为 /root/customersrowpattern 标识要处理的 <customers> 节点。

没有指定可选的 colpattern(列模式),因为列名和 xml 特性名称匹配。

openxml 行集提供程序创建了一个双列行集(customerid contactname),select 语句从该行集中检索必要的列(在本例中检索所有的列)。

declare @idoc int
declare @doc varchar(1000)
set @doc ='
<root>
<customer customerid="vinet" contactname="paul henriot">
   <order customerid="vinet" employeeid="5" orderdate="1996-07-04t00:00:00">
      <orderdetail orderid="10248" productid="11" quantity="12"/>
      <orderdetail orderid="10248" productid="42" quantity="10"/>
   </order>
</customer>
<customer customerid="lilas" contactname="carlos gonzlez">
   <order customerid="lilas" employeeid="3" orderdate="1996-08-16t00:00:00">
      <orderdetail orderid="10283" productid="72" quantity="3"/>
   </order>
</customer>
</root>'
--create an internal representation of the xml document.
exec sp_xml_preparedocument @idoc output, @doc
-- execute a select statement that uses the openxml rowset provider.
select    *
from       openxml (@idoc, '/root/customer',1)
            with (customerid  varchar(10),
                  contactname varchar(20))

下面是结果集:

customerid contactname          
---------- -------------------- 
vinet      paul henriot
lilas      carlos gonzlez

如果将 flags 设置为 2(表示以元素为中心的映射)并执行相同的 select 语句,由于 <customers> 元素没有任何子元素,则对于 xml 文档中两个 customer 的 customeridcontactname 的值都作为 null 返回。

下面是结果集:

customerid contactname
---------- -----------
null       null
null       null
b. 为列和 xml 特性之间的映射指定 colpattern

下面的查询从 xml 文档返回客户 id、订单日期、产品 id 和数量等特性。rowpattern 标识 <orderdetail> 元素。productidquantity 是 <orderdetails> 元素的特性。而 customeridorderdate 是父元素 (<orders>) 的特性。

指定可选的 colpattern,表示:

  • 行集中的 orderidcustomeridorderdate 列映射到 xml 文档中 rowpattern 所标识节点的父节点的特性。

  • 行集中的 prodid 列映射到 productid 特性,而行集中的 qty 列映射到 rowpattern 所标识节点的 quantity 特性。

尽管以元素为中心的映射由 flag 参数指定,但 colpattern 中指定的映射重写该映射。

declare @idoc int
declare @doc varchar(1000)
set @doc ='
<root>
<customer customerid="vinet" contactname="paul henriot">
   <order orderid="10248" customerid="vinet" employeeid="5" 
           orderdate="1996-07-04t00:00:00">
      <orderdetail productid="11" quantity="12"/>
      <orderdetail productid="42" quantity="10"/>
   </order>
</customer>
<customer customerid="lilas" contactname="carlos gonzlez">
   <order orderid="10283" customerid="lilas" employeeid="3" 
           orderdate="1996-08-16t00:00:00">
      <orderdetail productid="72" quantity="3"/>
   </order>
</customer>
</root>'
--create an internal representation of the xml document.
exec sp_xml_preparedocument @idoc output, @doc
-- select stmt using openxml rowset provider
select *
from   openxml (@idoc, '/root/customer/order/orderdetail',2)
         with (orderid       int         '../@orderid',
               customerid  varchar(10) '../@customerid',
               orderdate   datetime    '../@orderdate',
               prodid      int         '@productid',
               qty         int         '@quantity')

结果如下:

orderid customerid           orderdate                 prodid    qty

------------------------------------------------------------------------

10248      vinet       1996-07-04 00:00:00.000   11      12
10248      vinet       1996-07-04 00:00:00.000   42      10
10283      lilas       1996-08-16 00:00:00.000   72      3
c. 获得边缘表格式的结果

在下例中,在 openxml 语句中未指定 with 子句。因此,openxml 所生成的行集具有边缘表格式。select 语句返回边缘表中的所有列。

下例中的示例 xml 文档由 <customer>、<order> 和 <order_0020_details> 元素组成。

首先调用 sp_xml_preparedocument 以获得文档句柄。此文档句柄传递到 openxml。

在 openxml 语句中

  • rowpattern (/root/customer) 标识要处理的 <customer> 节点。

本文关键:SQL
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top