当前位置: 代码迷 >> Sql Server >> 怎么替换xml中所有节点指定字符串如“AAA”替换为“BBB”,
  详细解决方案

怎么替换xml中所有节点指定字符串如“AAA”替换为“BBB”,

热度:28   发布时间:2016-04-27 12:28:50.0
如何替换xml中所有节点指定字符串如“AAA”替换为“BBB”,在线等!!
Table Name: Response
Field: ConfigXML
如何把Response表中ConfigXML字段中所有节点中字符串“AAA”替换为“BBB”

------解决方案--------------------
不懂 学习 友情UP
------解决方案--------------------
贴出测试数据
------解决方案--------------------
你完整表结构也贴出来,你的field字段什么类型?
------解决方案--------------------
SQL code
update Response set ConfigXML= replace(convert(varchar(max),ConfigXML) ,'AAA','BBB')
------解决方案--------------------
不懂,帮顶.
或者看这里有没有你需要的内容?

SQLServer2005 XML在T-SQL查询中的典型应用
http://topic.csdn.net/u/20081107/17/68aaf5a9-c596-4ab5-ae18-f3370b2ab35b.html
------解决方案--------------------
给个例子吧,确实比较麻烦。
6#的操作,有可能多替换掉相关内容。

SQL code
DECLARE @myDoc xmlSET @myDoc = '<root>    <item ID="1">        <title>Ajax实战</title>        <author>张洪举</author>    </item>    <item ID="2">        <title>ASP.NET实战</title>        <author>卢桂章</author>    </item></root>'SELECT @myDoc -- 更新ID为1的item中的title元素的文本SET @myDoc.modify(' replace value of (/root/item[@ID=1]/title/text())[1] with "Ajax实战攻略"')SELECT @myDoc -- 更新属性值SET @myDoc.modify(' replace value of (/root/item[@ID=2][email protected])[1] with "3"')SELECT @myDoc
------解决方案--------------------
SQL code
declare @t table(id int,x xml);insert @t select 1,N'<ConfigXML>                           <A>                             <QuestionResponse>                              <ContentType>FileUploadQuestion</ContentType>                              <Response>OneAAA</Response>                          </QuestionResponse>                         <QuestionResponse>                              <ContentType>FileUploadQuestion</ContentType>                              <Response>TwoAAA</Response>                          </QuestionResponse>                        </A>                           <B>                             <QuestionResponse>                              <ContentType>FileUploadQuestion</ContentType>                              <Response>1AAA</Response>                          </QuestionResponse>                         <QuestionResponse>                              <ContentType>FileUploadQuestion</ContentType>                              <Response>2AABA</Response>                          </QuestionResponse>                        </B>                        </ConfigXML>'insert @t select 2,N'<ConfigXML>                           <A>                             <QuestionResponse>                              <ContentType>FileUploadQuestion</ContentType>                              <Response>123ABC</Response>                          </QuestionResponse>                         <QuestionResponse>                              <ContentType>FileUploadQuestion</ContentType>                              <Response>12AAA44</Response>                          </QuestionResponse>                        </A>                           <B>                             <QuestionResponse>                              <ContentType>FileUploadQuestion</ContentType>                              <Response>BBCAABCAAADC</Response>                          </QuestionResponse>                         <QuestionResponse>                              <ContentType>FileUploadQuestion</ContentType>                              <Response>CCAFA</Response>                          </QuestionResponse>                        </B>                        </ConfigXML>'--更新WHILE EXISTS(SELECT * FROM @t             WHERE x.exist('//QuestionResponse/Response[contains(.,"AAA")]')=1)    UPDATE @t SET        x.modify('replace value of (//QuestionResponse/Response[contains(.,"AAA")]/text())[1]                     with "BBB"')SELECT * FROM @t;
  相关解决方案