日期:2014-05-17  浏览次数:20580 次

sql xml replace value of不解之处
1.实现的功能是把节点77替换成99

2.我的实现
declare @source varchar(2000),@xml xml
set @source='10,25,68,2,55,77,269'
set @xml=convert(xml,'<v>'+replace(@source,',','</v><v>')+'</v>')


declare @replaceStr varchar(20),@newStr varchar(20)
set @replaceStr = '77'
set @newStr='99'

if @xml.exist('/v/text()=sql:variable("@replaceStr")') = 1
set @xml.modify('replace value of (/v[text()=sql:variable("@replaceStr")]) with sql:variable("@newStr")')

select @xml
---执行时会发生如下错误,求大神如何修改
  XQuery [modify()]: The target of 'replace' must be at most one node, found 'element(v,xdt:untyped) *'

------解决方案--------------------
我靠,昨天回复的时候csdn 挂了

气死我了

SQL code

WHILE @xml.exist('(v[.= sql:variable("@replaceStr")])') = 1 
begin
set @xml.modify('replace value of (/v[.= sql:variable("@replaceStr")]/text())[1] with sql:variable("@newStr")')
    
END 
SELECT  @xml