I want to loop through xml nodes in SQL Server, and create a copy of each node containing ','.
For example, for the following xml :
declare @answerXML xml = '<answers><answer part="1">answer,test0</answer><answer part="1">answer,test1</answer></answers>'
I want to be modified to become the following :
declare @answerXML xml = '<answers><answer part="1">answer,test0</answer><answer part="1">answer, test0</answer><answer part="1">answer,test1</answer><answer part="1">answer, test1</answer></answers>'
(Each node is duplicated, and in the added node, a space is added after the comma).
I was planing in using something like this :
SELECT
T.ref.value('.', 'varchar(256)') AS Answer
FROM
(SELECT
[Xml] = @answerXML.query('for $i in data(/answers/answer)
return element temp { $i }')
) A
CROSS APPLY
A.Xml.nodes('/temp') T(ref)
But no use, it seems it's too complicated.
Can anyone help in how to loop and update XML in T-SQL?
Thank you in advance,
1:nrelated table with all valid aliases. In this case you can join the user's input to your alias table and find the corresponding value.