求XQuery的写法

Ki1381 2007-01-20 11:08:02
表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虽然好但貌似还是更适合做手册。
...全文
321 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
feixiangVB 2007-01-23
  • 打赏
  • 举报
回复
帮顶~~~~~
mschen 2007-01-23
  • 打赏
  • 举报
回复
-- 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
Ki1381 2007-01-23
  • 打赏
  • 举报
回复
谢谢!
完美人生2013 2007-01-20
  • 打赏
  • 举报
回复
没有安装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.

6,129

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 新技术前沿
社区管理员
  • 新技术前沿社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧