我正在尝试使用SQL Server 2005中的XQUERY更新保存在列中的XML。这是我需要更新数据的示例。

<Length>3</Length>
<Width>5</Width>
<Depth>6</Depth>
<Area xsi:nil="true" />
<Volume xsi:nil="true" />

我需要将面积和音量设置为来自其他表的值。我正在为更新创建CTE。我省略了其他逻辑,但是我已经证实了CTE是否包含更新的正确数据:

;with Volume (DocumentID, Volume) As
(
  Select DocumentID, Volume from tbl
)

我正在使用以下Xquery SQL语句尝试更新表。

UPDATE tbl_Archive
   SET XML.modify(' declare namespace x="http://www.redacted.com";
   replace value of  (/x:Document/x:Volume/text())[1]
   with sql:column("Volume.Volume")')
    From Volume where volume.documentID = tbl_Archive.DocumentID

我会受到1行的影响,但是当我看XML时,它没有改变,我无法弄清楚需要修复什么才能使其正常工作。如果有任何区别,则该节点是未型的。

有帮助吗?

解决方案

如果没有文字要替换,则更新无法工作。 /x:Document/x:Volume/text())[1] 将返回一个空套。

尝试插入...

UPDATE tbl_Archive
   SET XML.modify(' declare namespace x="http://www.redacted.com";
   insert text {sql:column("Volume.Volume")}
   as first into  (/x:Document/x:Volume)[1]')
    From Volume where volume.documentID = tbl_Archive.DocumentID

..然后您需要删除 nil="true" 属性..

这样的东西也许..

 update tbl_Archive set XML.modify('delete /*:Document/*:Volume[text()]/@xsi:nil')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top