SQLServer XML資料的五種基本操作

來源:互聯網
上載者:User

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內容,請參見下一篇文章。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.