求XQuery的写法,该如何解决
求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虽然好但貌似还是更适合做手册。
------解决方案--------------------
等下我再写个exist方法
------解决方案--------------------
--1. 找出所有Width > 1200的ID
select id from #tb where [col].exist('/ImageSize/Width[text()[1]>1200]')=1
其它的写exist 没有上面的简便。
表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虽然好但貌似还是更适合做手册。
------解决方案--------------------
--> 测试数据:#tb
IF OBJECT_ID('TEMPDB.DBO.#tb') IS NOT NULL DROP TABLE #tb
GO
CREATE TABLE #tb(id int identity,[col] xml)
INSERT #tb
select '<ImageSize> <Width> 1491 </Width> <Height> 738 </Height> </ImageSize>'
union all select '<ImageSize> <Width> 648 </Width> <Height> 864 </Height> </ImageSize>'
union all select '<ImageSize> <Width> 768 </Width> <Height> 1024 </Height> </ImageSize>'
union all select '<ImageSize> <Width> 682 </Width> <Height> 1024 </Height> </ImageSize>'
select * from #tb
--我想要实现如下功能:
--1. 找出所有Width > 1200的ID
select id from #tb where [col].value('(/ImageSize/Width)[1]','int') >1200
--2. 找出所有Width > Height的ID
select id from #tb where [col].value('(/ImageSize/Width)[1]','int') >[col].value('(/ImageSize/Height)[1]','int')
--3. 找出所有Width / Height = 3:4或4:3的ID
select id from #tb where [col].value('(/ImageSize/Width)[1]','int')/[col].value('(/ImageSize/Height)[1]','int')
in (3/4,4/3)
--4. 输出字段为 ID Width Height 这样的传统格式表
select id,
[col].value('(/ImageSize/Width)[1]','int') as Width,
[col].value('(/ImageSize/Height)[1]','int') as Height
from #tb
等下我再写个exist方法
------解决方案--------------------
--1. 找出所有Width > 1200的ID
select id from #tb where [col].exist('/ImageSize/Width[text()[1]>1200]')=1
其它的写exist 没有上面的简便。