SQL 中操作XML类型数据

xml数据以其灵活的数据储存方式受到广大开发人员喜爱。在sqlserver2005中存在xml数据类型。我们可以使用这样的语句对xml数据进行操作

首先定义一段xml 数据:

代码
declare @xmldata xml 
set @xmldata = ' 
<bookstore> 
<book category="COOKING"> 
  <title lang="en">Everyday Italian</title> 
  <author>Giada De Laurentiis</author> 
  <year>2005</year> 
  <price>30.00</price> 
</book> 
<book category="
select @xmldata.value('bookstore[1]/book[@category="CHILDREN"][1]/title[1]/@lang','varchar(2)')

 

"> 
  <title lang="jp">Harry Potter</title> 
  <author>J K. Rowling</author> 
  <year>2005</year> 
  <price>29.99</price> 
</book> 
<book category="WEB"> 
  <title lang="en">XQuery Kick Start</title> 
  <author>James McGovern</author> 
  <author>Per Bothner</author> 
  <author>Kurt Cagle</author> 
  <author>James Linn</author> 
  <author>Vaidyanathan Nagarajan</author> 
  <year>2003</year> 
  <price>49.99</price> 
</book> 
<book category="WEB"> 
  <title lang="cn">Learning XML</title> 
  <author>Erik T. Ray</author> 
  <year>2003</year> 
  <price>39.95</price> 
</book> 
</bookstore> 

 

 

1、查询:@xml . query(xpath) xpath 是匹配方式字符串。必须是完整的,非拼接而成的字符串。'.' 代表本身 '/'代表子节点 '//'代表后代 '*'代表任何

  查询bookstore下所有属性category="WEB"的book节点 :

  

select @xmldata . query ('./bookstore/book[@category="WEB"]')

 

      查询最后1个book 节点:

  
select @xmldata . query ('./bookstore/book[last()]')

 

    中括号中可以是数字,表示第几个节点。注意:这里的数字1表示第一个节点,尔非0表示第一个。也可以是这样:[position()<=2] 表示前2个节点

  查询所有存在lang属性的节点:(查询所有后代节点中包含属性lang的节点)

   

select @xmldata . query ('//*[@lang]')

 

    同时,查询可以包含条件:  查询所有price节点值大于30的price节点

 

select @xmldata . query ('//price[text() > 30]')

 

    或则更复杂的条件: 查询所有的book节点,条件是其子节点的title节点的lang属性值为"en" 并且 year节点的值为"2003"

 

select @xmldata . query ('//book[./title[@lang="en"] and ./year[text() = "2003"]]')

 

有时查询字符串可能包含一些参数:如下代码:我们可能期望通过这样的语句查询所有属性category="WEB"的book节点

 

declare @parm varchar(3)
set @parm = "WEB"
select @xmldata . query ('//book[@category = "'+@parm+'"]')

 

但是,这样是错误的。会返回这样的错误:xml 数据类型方法 "query" 的参数 1 必须是字符串文字。

通过:sql:variable("@parm")就能解决这样的问题:

 

select @xmldata . query ('//book[@category = sql:variable("@parm")]')

 

另外还存在这样的查询方式:

 

select @xmldata . query ('for $b in bookstore/book where $b/year[text() = "2003"] return ($b)')

 

 

select @xmldata . query ('for $b in bookstore/book where $b/author order by $b/price[1] descending return ($b)')

 

注意:第二行代码 order by 语句后必须是精确的列,所以要使用[1]。如若不然,如果$b下存在多个price节点,这样的查询则不是我们所需要的

  除 @xml . query(xpath) 方法外还存在

       . exist(xpath) 方法 :

         返回0或1,表示xpath所选择的内容是否存在

         . value(xpath,type):

        返回xpath所选择内容的值,在这里需要制定返回值的类型

 

为了使XML查询更加高效,我们可以在XML数据上创建索引,具体方法暂不记录。

 2、修改:@xml . modify(insert) | .modify(replace) | .modify(delete)

    将一段xml片段插入到制定节点后(前)after | before 

set @xmldata . modify('insert <test>Hello</test> after (//book)[last()]')

       修改掉price="39.95"的price节点的值为"50.00"

set @xmldata . modify('replace value of (/bookstore/book/price[text()="39.95"]/text())[1] with "50.00"')

      删除所有test节点

set @xmldata . modify('delete //test')

   添加属性:

set @xmldata .modify ('insert attribute testatt{"50"} into (/bookstore/book)[1]')

 

  修改属性:

set @xmldata .modify('replace value of (/bookstore/book/@testatt)[1] with "80"')

 

 

posted @ 2009-12-11 02:02  郭鹏  阅读(6287)  评论(1编辑  收藏  举报