SQL操作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="CHILDREN">
<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")]')


 sql:variable("@para")用在提供参数,

但有时在表里查询用 sql:column("ColumnName") 会更加方便,他是直接根据提供的列名称去进行查询,例如:

DECLARE @ClassXML XML
SELECT @ClassXML=[ConfigValue].query('./root/Game/Classs/Class') FROM TableName WHERE Type='animal'

SELECT @ClassXML.query('./Class[@id=sql:column("Class")]').value('./Class[1]/@name','NVARCHAR(50)') AS 'Class',

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

 

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

posted @ 2010-10-08 12:05  YoungPay  阅读(552)  评论(2编辑  收藏  举报