SQLServer XML數(shù)據(jù)的五種基本操作
更新時(shí)間:2009年07月22日 08:01:11 作者:
SQLServer XML數(shù)據(jù)的五種基本操作語(yǔ)句
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á)式對(duì)XML的節(jié)點(diǎn)進(jìn)行insert , update 和 delete 操作。
下面通過(guò)例子對(duì)上面的五種操作進(jìn)行說(shuō)明:
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)')
結(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é)果集分別為:
<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)
結(jié)果集分別為:
<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
關(guān)于modify內(nèi)容,請(qǐng)參見(jiàn)下一篇文章。
輸入為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á)式對(duì)XML的節(jié)點(diǎn)進(jìn)行insert , update 和 delete 操作。
下面通過(guò)例子對(duì)上面的五種操作進(jìn)行說(shuō)明:
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)')
結(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é)果集分別為:
<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)
結(jié)果集分別為:
<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
關(guān)于modify內(nèi)容,請(qǐng)參見(jiàn)下一篇文章。
您可能感興趣的文章:
- SQL Server解析XML數(shù)據(jù)的方法詳解
- SQL Server中的XML數(shù)據(jù)進(jìn)行insert、update、delete操作實(shí)現(xiàn)代碼
- SQLServer2005 XML數(shù)據(jù)操作代碼
- 關(guān)于SQLServer2005的學(xué)習(xí)筆記 XML的處理
- SQLSERVER 2005中使用sql語(yǔ)句對(duì)xml文件和其數(shù)據(jù)的進(jìn)行操作(很全面)
- SQL Server中的XML數(shù)據(jù)進(jìn)行insert、update、delete
- SQL?Server中的XML數(shù)據(jù)類型詳解
相關(guān)文章
Android實(shí)現(xiàn)矩形區(qū)域截屏的方法
對(duì)屏幕進(jìn)行截屏并裁剪有兩種方式:早截圖和晚截圖,對(duì)于早截圖和晚截圖的概念大家通過(guò)本文詳解學(xué)習(xí)。本文重點(diǎn)給大家介紹android實(shí)現(xiàn)矩形區(qū)域截屏的方法,需要的朋友參考下2017-01-01win2008 r2 安裝sql server 2005/2008 無(wú)法連接服務(wù)器解決方法
在與 SQL Server 建立連接時(shí)出現(xiàn)與網(wǎng)絡(luò)相關(guān)的或特定于實(shí)例的錯(cuò)誤。未找到或無(wú)法訪問(wèn)服務(wù)器。請(qǐng)驗(yàn)證實(shí)例名稱是否正確并且 SQL Server 已配置為允許遠(yuǎn)程連接2015-01-01親自教你使用?ChatGPT?編寫(xiě)?SQL?JOIN?查詢示例
這篇文章主要介紹了使用ChatGPT編寫(xiě)SQL?JOIN查詢,作為一種語(yǔ)言模型,ChatGPT 可以就如何構(gòu)建復(fù)雜的 SQL 查詢和 JOIN 提供指導(dǎo)和建議,但它不能直接訪問(wèn) SQL 數(shù)據(jù)庫(kù),它可以幫助您了解語(yǔ)法、最佳實(shí)踐和有關(guān)如何構(gòu)建查詢以高效執(zhí)行的一般指導(dǎo),需要的朋友可以參考下2023-02-02SQL?Server快速?gòu)氐仔遁d實(shí)例方法分享
最近在安裝了SQL Server后,當(dāng)由于某些原因我們需要卸載它時(shí),我們應(yīng)該怎么操作呢?這篇文章主要給大家介紹了關(guān)于SQL?Server快速?gòu)氐仔遁d的相關(guān)資料,需要的朋友可以參考下2023-10-10