You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何批量清空XML中所有Entry标签的value属性值?

最佳解决方案(SQL Server 环境)

可以通过在XQuery的modify方法中使用for循环遍历所有目标属性来实现批量清空,既解决了返回多结果的问题,也完全规避SQL注入风险。

具体代码示例

假设XML数据存储在表YourTable的XmlColumn列中,主键为Id,执行以下更新语句:

UPDATE YourTable
SET XmlColumn.modify('
  for $attr in /Attributes/Entry/@value
  return replace value of $attr with ""
')
WHERE Id = @YourId; -- 根据实际业务条件筛选目标行

代码说明

  • for $attr in /Attributes/Entry/@value:遍历所有Entry标签下的value属性,将每个属性赋值给变量$attr
  • replace value of $attr with "":对每个遍历到的属性执行清空操作
  • 整个语句会一次性处理所有符合条件的属性,无需依赖外部变量,也不会返回多结果

测试验证

用你提供的XML数据测试:
原XML:

<Attributes>
  <Entry key="T2" value="r1" />
  <Entry key="Test" value="s1" />
</Attributes>

执行语句后得到目标结果:

<Attributes>
  <Entry key="T2" value="" />
  <Entry key="Test" value="" />
</Attributes>

内容的提问来源于stack exchange,提问作者Sergey Alikin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 13:24:58