所以我有一个脚本可以将以下节点的 XML 值更新/插入为 True:
<Submitted>False</Submitted>
问题不是所有行都包含节点,因此,它会引发错误:“不能在空值上调用 '@temp' 上的 Mutator 'modify()'。”
我需要做什么来过滤掉 XML 中不包含“提交”节点的行?
**注意,我有所有这些疯狂的 CASTS,因为列类型是 TEXT 并且无法更改,因为客户端最初是这样设置的。
DECLARE @temp XML
SELECT
@temp = CAST(CAST(TicorOregon..tbl_Module_RequestForms_Items.XML AS NTEXT) AS XML)
FROM
TicorOregon..tbl_Module_RequestForms_Items
WHERE
CAST(CAST(TicorOregon..tbl_Module_RequestForms_Items.XML AS NTEXT) AS XML).value('(//Record/Submitted)[1]', 'NVARCHAR(max)') <> 'True'
-- modification to local XML var
SET
@temp.modify('replace value of (//Record/Submitted[1]/text())[1] with "True"')
-- write it back into the table as TEXT column
UPDATE
TicorOregon..tbl_Module_RequestForms_Items
SET
XML = CAST(CAST(@temp AS VARCHAR(MAX)) AS TEXT)
WHERE
CAST(CAST(TicorOregon..tbl_Module_RequestForms_Items.XML AS NTEXT) AS XML).value('(//Record/Submitted)[1]', 'NVARCHAR(max)') <> 'True'
AND CAST(CAST(TicorOregon..tbl_Module_RequestForms_Items.XML AS NTEXT) AS XML).value('(//Record/Submitted)[1]', 'NVARCHAR(max)') <> null