I have a table with a column ID and a column XMLData. Unfortunately, the XML data column does not currently have any reference to the ID inside the XML itself, so if I export this column only and parse it, I would not be able to tie it back to the ID.
I am trying to added a node to the XML similar to below:
<AddedNode>
<Id>10862832</Id>
</AddedNode>
Which would allow for the ID to be parsed from the XML itself, and be connected back. I tried adding the data via the below method:
With Test_CTE (Id, XMLData, AddedXML)
As
(
select Id, XMLData, cast('<AddedNode>
<Id>' + cast(Id as varchar(20)) + '</Id>
</AddedNode>' as varchar(max)) as AddedXML
from dbo.TestTable
)
update T
set XMLData.modify('insert ' + CTE.AddedXML + '
as last into (/RootNode[1])
')
from dbo.TestTable T
inner join Test_CTE CTE on CTE.Id = T.Id
But I receive the error "The argument 1 of the XML data type method "modify" must be a string literal."
I'm sure this has to do with me trying to pull the insert node via a column of data, but could anyone tell me how to fix it?
NOTE: I realize that I could also complete this via a while loop over entire table using variables, but there are ~2.5 million rows in the table and it would take longer than I want.