SQL 中對XML資料的修改

來源:互聯網
上載者:User

 

 

 

今天雲次那個資料出錯, 一看,靠,原來的指令碼Bat改稱cmd了。只能修改資料了。

原來的資料:

<VStormCliCreator>  <MachineName>B1-Site</MachineName>  <NIC>TrunkNetworkSwitch</NIC>  <Host>MSD-1531346.fareast.corp.microsoft.com</Host>  <Memory>4096</Memory>  <Password>User@123</Password>  <VLanID>979</VLanID>  <PostScripts>    <PostScript>\\172.23.93.230\scratch$\v-XXX\Run.bat</PostScript>  </PostScripts>  <ImageName>97-SP2</ImageName>  <OSID>97</OSID></VStormCliCreator>

 

修改命令:

update dbo.MachineConfigs set CreatorConfig.modify('replace value of 
(/VStormCliCreator/PostScripts/PostScript/text())[1] with ("\\172.23.93.230\scratch$\v-XXX\Run.cmd")') 
where ID = '49E6303D-577D-43C8-BB57-0533FC19BBEC' select * from dbo.MachineConfigs  where ID = '49E6303D-577D-43C8-BB57-0533FC19BBEC'

 

該後的資料:

<VStormCliCreator>  <MachineName>B1-Site</MachineName>  <NIC>TrunkNetworkSwitch</NIC>  <Host>MSD-1531346.fareast.corp.microsoft.com</Host>  <Memory>4096</Memory>  <Password>User@123</Password>  <VLanID>979</VLanID>  <PostScripts>    <PostScript>\\172.23.93.230\scratch$\v-XXX\Run.cmd</PostScript>  </PostScripts>  <ImageName>97-SP2</ImageName>  <OSID>97</OSID></VStormCliCreator>
 
著重感謝 郭勇成的《如何對SQL Server中的XML資料進行insert Update delete》 http://blog.csdn.net/tjvictor/archive/2009/07/21/4368496.aspx

modify方法

該方法可以對 XML資料進行更新。通過XQuery中添加的insert、delete和update關鍵字提供了對XML DML的支援,使用 insert、delete和update關鍵字可以分別插入、刪除和更新一個或多個節點。例如,在查詢時段中輸入以下代碼:

UPDATE books SET xmlCol.modify(  'insert  <section num="1">  <content>Background</content> </section> after (/book/title)[1]')  where id = 1

在該段代碼的第1行使用UPDATE語句修改books表中xmlCol類型的欄位,在第2~7行使用insert

代碼
 1 <book type="computer" publicationdate="2008" ISBN="0-7356-1588-2">   
2 <title>c#</title>
3  <author>
4 <first-name>sheng</first-name>
5 <last-name>bin</last-name>
6 </author>
7 <author>
8 <first-name>gengxin</first-name>
9 <last-name>sun</last-name>
10 </author>
11 <price>35.99</price>
12 </book>

 

執行此段代碼後,在id列值為1的記錄的xmlCol列中添加元素section,此元素將添加在title元素的下面。更改後的xmlCol列的內容將如下所示。

代碼
 1 <book type="computer" publicationdate="2008" ISBN="0-7356-1588-2">    
2 <title>c#</title>
3 <section num="1">
4 <content>Background</content>
5 </section>
6 <author>
7 <first-name>sheng</first-name>
8 <last-name>bin</last-name>
9 </author>
10 <author>
11 <first-name>gengxin</first-name>
12 <last-name>sun</last-name>
13 </author>
14 <price>35.99</price>
15 </book>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.