当前位置: 移动技术网 > IT编程>数据库>MSSQL > SQLServer XML数据的五种基本操作

SQLServer XML数据的五种基本操作

2017年12月12日  | 移动技术网IT编程  | 我要评论

电子技术论坛,救护车被卡桥下,truecolors

1.xml.exist
输入为xquery表达式,返回0,1或是null。0表示不存在,1表示存在,null表示输入为空
2.xml.value
输入为xquery表达式,返回一个sql server标量值
3.xml.query
输入为xquery表达式,返回一个sql server xml类型流
4.xml.nodes
输入为xquery表达式,返回一个xml格式文档的一列行集
5.xml.modify
使用xquery表达式对xml的节点进行insert , update 和 delete 操作。
下面通过例子对上面的五种操作进行说明:
declare @xmlvar xml = '
<catalog>
<book category="itpro">
<title>windows step by step</title>
<author>bill zack</author>
<price>49.99</price>
</book>
<book category="developer">
<title>developing ado .net</title>
<author>andrew brust</author>
<price>39.93</price>
</book>
<book category="itpro">
<title>windows cluster server</title>
<author>stephen forte</author>
<price>59.99</price>
</book>
</catalog>'
1. xml.exist
select @xmlvar.exist('/catalog/book')-----返回1
select @xmlvar.exist('/catalog/book/@category')-----返回1
select @xmlvar.exist('/catalog/book1')-----返回0
set @xmlvar = null
select @xmlvar.exist('/catalog/book')-----返回null
2.xml.value
select @xmlvar.value('/catalog[1]/book[1]','varchar(max)')
select @xmlvar.value('/catalog[1]/book[2]/@category','varchar(max)')
select @xmlvar.value('/catalog[2]/book[1]','varchar(max)')
结果集为:
windows step by stepbill zack49.99 developer null
3.xml.query
select @xmlvar.query('/catalog[1]/book')
select @xmlvar.query('/catalog[1]/book[1]')
select @xmlvar.query('/catalog[1]/book[2]/author')
结果集分别为:
<book category="itpro">
<title>windows step by step</title>
<author>bill zack</author>
<price>49.99</price>
</book>
<book category="developer">
<title>developing ado .net</title>
<author>andrew brust</author>
<price>39.93</price>
</book>
<book category="itpro">
<title>windows cluster server</title>
<author>stephen forte</author>
<price>59.99</price>
</book>
<book category="itpro">
<title>windows step by step</title>
<author>bill zack</author>
<price>49.99</price>
</book>
<author>andrew brust</author>
4.xml.nodes
select t.c.query('.') as result from @xmlvar.nodes('/catalog/book') as t(c)
select t.c.query('title') as result from @xmlvar.nodes('/catalog/book') as t(c)
结果集分别为:
<book category="itpro"><title>windows step by step</title><author>bill …………
<book category="developer"><title>developing ado .net</title><author>andrew …………
<book category="itpro"><title>windows cluster server</title><author>stephen …………
<title>windows step by step</title>
<title>developing ado .net</title>
<title>windows cluster server</title>
5.xml.modify
关于modify内容,请参见下一篇文章。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网