sqlserver2005 xml字段的读写操作
2010-05-20 12:41
344 查看
1.创建xml字段
在表xmlTest内创建一个字段,命名_x,设置数据类型为xml。
2.写入xml
insert into xmlTest (_x) values('
<root>
<item attr1="a1" attr2="a2">item11</item>
<item attr1="a1" attr2="a2">item12</item>
</root>
')
go
如此再插入一条记录:
insert into xmlTest (_x) values('
<root>
<item attr1="b1" attr2="b2">item21</item>
<item attr1="b1" attr2="b2">item22</item>
</root>
')
3.查询数据
3.1 查询字段所有数据
select _x from xmlText
3.2 查询root下面的第一个item节点
select _x.query('root/item[1]')
from xmlTest
3.3 查询root下面的第一个item节点的值
select _x.value('(root/item)[1]','varchar(50)')
from xmlTest
3.3 查询root下面的第二个item节点的attr2属性值
select _x.value('(mailto:root/item/@attr2)%5B2%5D%27,%27varchar(50)%27)
from xmlTest
3.4 查询表中每条记录的xml字段的item节点值,全部列出来
SELECT t2.c1.value('.','varchar(20)') as items
from xmlTest
cross apply _x.nodes('root/item') as t2(c1)
3.5 查询表中id=1记录的xml的所有item字段的attr2属性值
SELECT t2.c1.value(mailto:%27@attr2%27,%27varchar(20)%27) as attr2
from xmlTest
cross apply _x.nodes('root/item') as t2(c1)
where id='1'
3.6 查询id为1的所有节点中attr1属性为a1的第一个匹配节点的attr2属性值
select _x.value('(root/item[@attr1="a1"]/@attr2)[1]','varchar(50)')
from xmlTest where id='1'
3.7 多表查询
例如:存在另外1个table:t2,其字段xmlid关联表xmlTest,而attr1关联xmlTest的_x字段中的attr1属性值。
查询表xmlTest中id=‘1'记录中_x字段内item节点中attr1为表t2中attr1字段值时,item节点中attr2的属性值
select xmlTest._x.value('(root/item[@attr1=sql:column("t2.attr1")]/@attr2)[1]','varchar(50)') as attr2,t2.attr1
from xmlTest join t2 on t2.xmlid=xmlTest.id
where xmlTest.id='1'
4.修改数据
4.1 修改表中id=1记录的第一个item节点值
UPDATE xmlTest
set _x.modify('replace value of (root/item/text())[1] with "xxxxx"')
where id='1'
select _x.query('root/item[1]') as item1
from xmlTest
4.2 修改表中id=2记录的第二个item节点的attr2属性值
UPDATE xmlTest
set _x.modify('replace value of (mailto:root/item/@attr2)%5B2] with "2222b"')
where id='2'
select _x.query('root/item[2]') as item1
from xmlTest
原文地址 : http://www.cnblogs.com/Real_Dream/articles/1586005.html
在表xmlTest内创建一个字段,命名_x,设置数据类型为xml。
2.写入xml
insert into xmlTest (_x) values('
<root>
<item attr1="a1" attr2="a2">item11</item>
<item attr1="a1" attr2="a2">item12</item>
</root>
')
go
如此再插入一条记录:
insert into xmlTest (_x) values('
<root>
<item attr1="b1" attr2="b2">item21</item>
<item attr1="b1" attr2="b2">item22</item>
</root>
')
3.查询数据
3.1 查询字段所有数据
select _x from xmlText
3.2 查询root下面的第一个item节点
select _x.query('root/item[1]')
from xmlTest
3.3 查询root下面的第一个item节点的值
select _x.value('(root/item)[1]','varchar(50)')
from xmlTest
3.3 查询root下面的第二个item节点的attr2属性值
select _x.value('(mailto:root/item/@attr2)%5B2%5D%27,%27varchar(50)%27)
from xmlTest
3.4 查询表中每条记录的xml字段的item节点值,全部列出来
SELECT t2.c1.value('.','varchar(20)') as items
from xmlTest
cross apply _x.nodes('root/item') as t2(c1)
3.5 查询表中id=1记录的xml的所有item字段的attr2属性值
SELECT t2.c1.value(mailto:%27@attr2%27,%27varchar(20)%27) as attr2
from xmlTest
cross apply _x.nodes('root/item') as t2(c1)
where id='1'
3.6 查询id为1的所有节点中attr1属性为a1的第一个匹配节点的attr2属性值
select _x.value('(root/item[@attr1="a1"]/@attr2)[1]','varchar(50)')
from xmlTest where id='1'
3.7 多表查询
例如:存在另外1个table:t2,其字段xmlid关联表xmlTest,而attr1关联xmlTest的_x字段中的attr1属性值。
查询表xmlTest中id=‘1'记录中_x字段内item节点中attr1为表t2中attr1字段值时,item节点中attr2的属性值
select xmlTest._x.value('(root/item[@attr1=sql:column("t2.attr1")]/@attr2)[1]','varchar(50)') as attr2,t2.attr1
from xmlTest join t2 on t2.xmlid=xmlTest.id
where xmlTest.id='1'
4.修改数据
4.1 修改表中id=1记录的第一个item节点值
UPDATE xmlTest
set _x.modify('replace value of (root/item/text())[1] with "xxxxx"')
where id='1'
select _x.query('root/item[1]') as item1
from xmlTest
4.2 修改表中id=2记录的第二个item节点的attr2属性值
UPDATE xmlTest
set _x.modify('replace value of (mailto:root/item/@attr2)%5B2] with "2222b"')
where id='2'
select _x.query('root/item[2]') as item1
from xmlTest
原文地址 : http://www.cnblogs.com/Real_Dream/articles/1586005.html
相关文章推荐
- sqlserver2005 xml字段的读写操作
- sqlserver2005 xml字段的读写操作
- sqlserver2005 xml字段的读写操作
- sqlserver 2005 xml字段的读写操作
- sqlserver2005提供的xml数据类型操作xml串
- opencv 学习笔记2—XML读写操作
- Qt学习之XML读写操作小结
- Python 中对XML 的基本的读写操作
- opencv学习笔记(二)-对xml和yaml文件的读写操作
- hibernate在SSH框架操作ORACLE Clob字段类型的读写改
- SQL对Xml字段的操作
- Django manytomany 字段的读写操作及总结
- Javascript 读取操作Sql中的Xml字段
- .Net那点事儿系列:C#操作Xml:通过XmlDocument读写Xml文档
- C#不使用DataSet操作XML,XmlDocument读写xml所有节点及读取xml节点的数据总结
- SQL2008 操作XML 单字段 变量
- c#_xml读写_操作_实例
- 实现动态的XML文件读写操作(依然带干货)
- 如何完成.Net下XML文档的读写操作(转)
- C#创建、读写、增加、删除XML操作