Hi @T.Zacks ,
Please try the following solution.
You need to provide a minimal reproducible example.
I removed unnecessary XML elements irrelevant to the task.
SQL Server XQuery .modify()
method can update just one XML element at a time. That's why I am using a loop to update multiple XML elements in question.
XQuery version 3.0/3.1 can update multiple XML elements in one single shot.
Microsoft yet to implement it support in MS SQL Server.
That's one of the reasons for my suggestion to Microsoft: SQL Server vNext (post 2019) and NoSQL functionality
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO @tbl (xmldata) VALUES
(N'<ArrayOfClsCommentPopup xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<clsCommentPopup>
<LineItem>Total Revenue</LineItem>
</clsCommentPopup>
<clsCommentPopup>
<LineItem>Total Revenue</LineItem>
</clsCommentPopup>
</ArrayOfClsCommentPopup>');
-- DDL and sample data population, end
DECLARE @from VARCHAR(30) = 'Total Revenue'
, @to VARCHAR(30) = 'Gross Revenue';
-- before
SELECT * FROM @tbl
WHERE xmldata.exist('/ArrayOfClsCommentPopup/clsCommentPopup/LineItem[text() = sql:variable("@from")]') = 1;
DECLARE @UPDATE_STATUS BIT = 1;
WHILE @UPDATE_STATUS > 0
BEGIN
UPDATE t
SET xmldata.modify('replace value of (/ArrayOfClsCommentPopup/clsCommentPopup/LineItem[text() = sql:variable("@from")]/text())[1]
with (sql:variable("@to"))')
FROM @tbl AS t
WHERE xmldata.exist('/ArrayOfClsCommentPopup/clsCommentPopup/LineItem[text() = sql:variable("@from")]') = 1;
SET @UPDATE_STATUS = @@ROWCOUNT;
PRINT @UPDATE_STATUS;
END;
-- after
SELECT * FROM @tbl;