如何使用modify在SQL Server中更新XML列?

huangapple go评论55阅读模式
英文:

How do I update an XML column in SQL Server using modify?

问题

我有一个SQL Server表的XML数据类型列中的以下值:

<feed>
  <schedule>
    <arrivalSla dayOfWeek="monday" addDays="1" time="02:00:00"/>
	<arrivalSla dayOfWeek="tuesday" addDays="1" time="02:00:00"/>
	<arrivalSla dayOfWeek="wednesday" addDays="1" time="02:00:00"/>
	<arrivalSla dayOfWeek="thursday" addDays="1" time="02:00:00"/>
	<arrivalSla dayOfWeek="friday" addDays="3" time="12:45:00"/>
  </schedule>
  <assetCode value="FI" />
</feed>

我想要将dayOfWeek="friday"的时间属性更新为"11:00:00"。我尝试了各种方法,包括以下方法,但出现以下错误:

XQuery [learnTheXML.xnlColumn.modify()]: The target of 'replace' must be at most one node, found 'element(arrivalSla,xdt:untyped) *'

UPDATE learnTheXML
SET xmlColumn.modify('replace value of (/feed/schedule/arrivalSla[@dayOfWeek="friday"]) with "11:00:00"')
WHERE id = 1

我做错了什么?

英文:

I have the following value in an XML data type column of a SQL Server table:

<feed>
  <schedule>
    <arrivalSla dayOfWeek="monday" addDays="1" time="02:00:00"/>
	<arrivalSla dayOfWeek="tuesday" addDays="1" time="02:00:00"/>
	<arrivalSla dayOfWeek="wednesday" addDays="1" time="02:00:00"/>
	<arrivalSla dayOfWeek="thursday" addDays="1" time="02:00:00"/>
	<arrivalSla dayOfWeek="friday" addDays="3" time="12:45:00"/>
  </schedule>
  <assetCode value="FI" />
</feed>

I want to update the time attribute where dayOfWeek="friday" to "11:00:00". I have tried various ways, including the following, but get an error of

> XQuery [learnTheXML.xnlColumn.modify()]: The target of 'replace' must be at most one node, found 'element(arrivalSla,xdt:untyped) *'

UPDATE learnTheXML
SET xmlColumn.modify('replace value of (/feed/schedule/arrivalSla[@dayOfWeek="friday"]) with "11:00:00"')
WHERE id = 1

What am I doing wrong?

答案1

得分: 1

你仍然需要提供一个单一数值来更新,并且你需要告诉它你想要更新(大概是)time的值:

UPDATE learnTheXML
SET xmlColumn.modify('replace value of (/feed/schedule/arrivalSla[@dayOfWeek="friday"]/@time)[1] with "11:00:00"')
WHERE id = 1;
英文:

You still need to provide a singleton value to update and also you need to tell it you want to update (presumably) the value of time:

UPDATE learnTheXML
SET xmlColumn.modify('replace value of (/feed/schedule/arrivalSla[@dayOfWeek="friday"]/@time)[1] with "11:00:00"')
WHERE id = 1;

huangapple
  • 本文由 发表于 2023年2月18日 01:51:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定