无法在SQL Server的xml中一次替换多个值,有几种选择:
- 使用循环并一一更新属性
- 将数据拆分为临时表/表变量,进行更新,然后合并为一个xml
- 使用xquery重建xml
我认为正确的方法是循环解决方案:
select @i = @data.value('count(//Attribute[DataType="Float" and Format="n0"]/Format)', 'int')while @i > 0begin set @data.modify(' replace value of (//Attribute[DataType="Float" and Format="n0"]/Format/text())[1] with "f2" ') set @i = @i - 1end[sql fiddle demo](http://sqlfiddle.com/#!3/d41d8/23529)
如果您的xml包含namepaces,我发现的最简单的更新方法是声明
default名称空间:
;with xmlnamespaces(default 'schemas.microsoft.com/sqlserver/2004/10/semanticmodeling')select @i = @xml.value('count(//Attribute[DataType="Float" and Format="n0"]/Format)', 'int')while @i > 0begin set @xml.modify(' declare default element namespace "schemas.microsoft.com/sqlserver/2004/10/semanticmodeling"; replace value of (//Attribute[DataType="Float" and Format="n0"]/Format/text())[1] with "f2" ') set @i = @i - 1endselect @xml


