【问题标题】:Using Xquery to replace a node value that is xsi:nil = "true"使用 Xquery 替换节点值为 xsi:nil = "true"
【发布时间】:2010-12-14 22:22:38
【问题描述】:

我正在尝试使用 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 时,它并没有改变,我不知道需要修复什么才能使其正常工作。该节点是无类型的,如果这有什么不同的话。

【问题讨论】:

    标签: sql-server xml xml-dml xml-nil


    【解决方案1】:

    如果没有要替换的文本,更新将不起作用。XPath /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')
    

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      相关资源
      最近更新 更多