您的位置:首页 > 数据库

SqlServer2005 操作XML 实战

2007-10-26 21:07 274 查看
XML 大兴其道。文件越来越大。在SqlServer中查询检索,会使工作得心应手。

常用SQL:

1. 提取关心数据。以表行的形式列出。要注意的是。Xquery在解析 XML 文档里,无素内容是和关键字是区分大小写的。

create table #t (id int ,xm xml)
insert into #t(id,xm)
select 1 , * from OpenRowSet( Bulk 'D:\Program Files\StormII\config.xml', Single_Blob ) as x
--只关心 其他视频/音频文件
insert into #t(id,xm)
select 2,xm.query('for $f in /config/association/type[@category="其他视频/音频文件"] return $f') as result from #t

declare @x xml
select @x=xm from #t where id = 2
insert into #t select 3,t.c.query('.')from @x.nodes('/type/item') t(c)

select xm.value('(/item/@format)[1]','varchar(30)' ) as format ,xm.value('(/item/@description)[1]','varchar(30)' ) as description
from #t where id = 3

go

2.XML中的查询。既然已经查出了表的结果集,就可以按表来操作,进行查询。如:查询 其它视频/音频文件 中包含音频的。

select xm.value('(/item/@format)[1]','varchar(30)' ) as format ,xm.value('(/item/@description)[1]','varchar(30)' ) as description
from #t where id = 3
and xm.value('(/item/@description)[1]','varchar(30)' ) like '%音频%'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: