Maison  >  Article  >  base de données  >  SQLServer XML数据的五种基本操作

SQLServer XML数据的五种基本操作

WBOY
WBOYoriginal
2016-06-07 17:58:42728parcourir

SQLServer XML数据的五种基本操作语句

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 = '


Windows Step By Step
Bill Zack
49.99


Developing ADO .NET
Andrew Brust
39.93


Windows Cluster Server
Stephen Forte
59.99

'
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')
结果集分别为:

Windows Step By Step
Bill Zack
49.99


Developing ADO .NET
Andrew Brust
39.93


Windows Cluster Server
Stephen Forte
59.99


Windows Step By Step
Bill Zack
49.99

Andrew Brust
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)
结果集分别为:
Windows Step By Step Bill …………
Developing ADO .NET Andrew …………
Windows Cluster Server Stephen …………
Windows Step By Step
Developing ADO .NET
Windows Cluster Server
5.xml.modify
关于modify内容,请参见下一篇文章。
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn