日期:2014-05-18  浏览次数:20597 次

求XQuery的写法
表T中有字段F(xml类型)和字段ID(数值型),现在有以下这么
几条记录,其中F字段的内容为:

<ImageSize> <Width> 1491 </Width> <Height> 738 </Height> </ImageSize>
<ImageSize> <Width> 648 </Width> <Height> 864 </Height> </ImageSize>
<ImageSize> <Width> 768 </Width> <Height> 1024 </Height> </ImageSize>
<ImageSize> <Width> 682 </Width> <Height> 1024 </Height> </ImageSize>

我想要实现如下功能:

1.   找出所有Width   >   1200的ID
2.   找出所有Width   >   Height的ID
3.   找出所有Width   /   Height   =   3:4或4:3的ID
4.   输出字段为   ID   Width   Height   这样的传统格式表


我xquery菜鸟一个,还请各位高手多多指教!另问,有什么比较适合零基础的xQuery方面的入门书或资料?MSDN虽然好但貌似还是更适合做手册。

------解决方案--------------------
没有安装SQL Server 2005,不过之前看过一些,其实就是XML的XPath查询而已。

XPath的一些例子:

author[(degree or award) and publication]
All <author> elements that contain at least one <degree> or <award> and at least one <publication> as the children
author[degree and not(publication)]
All <author> elements that contain at least one <degree> element child and that contain no <publication> element children.
author[not(degree or award) and publication]
All <author> elements that contain at least one <publication> element child and contain neither <degree> nor <award> element children.
author[last-name = "Bob "] All <author> elements that contain at least one <last-name> element child with the value Bob.

------解决方案--------------------
-- SQL SERVER 2005.创建测试表
create table T([ID] int,F xml)
Go
-- 插入测试数据
insert T select 1, ' <ImageSize> <Width> 1491 </Width> <Height> 738 </Height> </ImageSize> '
insert T select 2, ' <ImageSize> <Width> 648 </Width> <Height> 864 </Height> </ImageSize> '
insert T select 3, ' <ImageSize> <Width> 768 </Width> <Height> 1024 </Height> </ImageSize> '
insert T select 4, ' <ImageSize> <Width> 682 </Width> <Height> 1024 </Height> </ImageSize> '
Go
--1. 找出所有Width > 1200的ID
select *
from T
where F.value( '(/ImageSize/Width)[1] ', 'int ')> 1200
--2.找出所有Width > Height的ID
select *
from T
where F.value( '(/ImageSize/Width)[1] ', 'int ')> F.value( '(/ImageSize/Height)[1] ', 'int ')
--3. 找出所有Width / Height = 3:4或4:3的ID
select *
from T
where F.value( '(/ImageSize/Width)[1] ', 'int ')/F.value( '(/ImageSize/Height)[1] ', 'int ') in (3/4,4/3)
--4.输出字段为 ID Width Height 这样的传统格式表
select [ID],
F.value( '(/ImageSize/Width)[1] ', 'int ') as [Width],
F.value( '(/ImageSize/Height)[1] ', 'int ') as [Height]
from T

------解决方案--------------------
帮顶~~~~~