SQL 中对XML数据的修改

今天云次那个数据出错, 一看,靠,原来的脚本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>
原文地址:https://www.cnblogs.com/jimson/p/Sql2.html