SqlServer2005 操作XML 实战

XML 大兴其道。文件越来越大。在SqlServer中查询检索,会使工作得心应手。

常用SQL:

1. 提取关心数据。以表行的形式列出。要注意的是。Xquery在解析 XML 文档里,无素内容是和关键字是区分大小写的。
create table #t (id int ,xm xml)
insert into #t(id,xm)
select 1 , * from OpenRowSetBulk '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 '%音频%'

原文地址:https://www.cnblogs.com/newsea/p/939151.html