栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

修改(替换)条件的XML

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

修改(替换)条件的XML

无法在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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/614488.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号