SQLServer XML數(shù)據(jù)的五種基本操作

sqlserver XML數(shù)據(jù)的五種基本操作語句

1.xml.exist
輸入為XQuery表達(dá)式,返回0,1或是Null。0表示不存在,1表示存在,Null表示輸入為空
2.xml.value
輸入為XQuery表達(dá)式,返回一個(gè)SQL Server標(biāo)量值
3.xml.query
輸入為XQuery表達(dá)式,返回一個(gè)SQL Server XML類型流
4.xml.nodes
輸入為XQuery表達(dá)式,返回一個(gè)XML格式文檔的一列行集
5.xml.modify
使用XQuery表達(dá)式對XML的節(jié)點(diǎn)進(jìn)行insert , update 和 delete 操作。
下面通過例子對上面的五種操作進(jìn)行說明:
declare @XMLVar xml = ‘


Windows Step By Step
Bill Zack49.99


Developing ADO .NET
Andrew Brust39.93


Windows Cluster Server
Stephen Forte59.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)’)
結(jié)果集為:
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’)
結(jié)果集分別為:

Windows Step By Step
Bill Zack49.99


Developing ADO .NET
Andrew Brust39.93


Windows Cluster Server
Stephen Forte59.99


Windows Step By Step
Bill Zack49.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)
結(jié)果集分別為:
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
關(guān)于modify內(nèi)容,請參見下一篇文章。

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊8 分享