<metadata>
<layout>8</layout>
<maxScore>7</maxScore>
</metadata>
我确实想要为列为XML类型的表中的多个记录替换<maxscore>
的值。并且<maxscore>
替换值在临时变量中被接收为@MaxReplace。
尝试如下。
DECLARE @MaxReplace as NVARCHAR(10)
SET @MaxReplace='1'
Update dbo.SCORETBALE
SET [XML].modify('replace value of (/ns:metadata/ns:maxScore)[1] with "@MaxReplace "')
FROM dbo.XYZ...(condition)...
答案 0 :(得分:1)
您需要首先选择元素并使用sql:variable(...)
来绑定变量中的值:
CREATE TABLE #SCORETBALE(col XML);
INSERT INTO #SCORETBALE(coL)
VALUES ('<metadata>
<layout>8</layout>
<maxScore>1</maxScore>
</metadata>');
DECLARE @MaxReplace AS NVARCHAR(10) = '22';
UPDATE #SCORETBALE
SET col.modify('replace value of (/metadata/maxScore/text())[1]
with sql:variable("@MaxReplace")')
-- WHERE ...
SELECT *
FROM #SCORETBALE;
的 LiveDemo
强>