Adding node to XML data using Xquery

Is there a way to add a node to an XML string using Xquery?

For example:


The XML string is stored in a SQL table so I would need to extract the string, add the node and save the string back to the table.

Who is Participating?
Kevin CrossConnect With a Mentor Chief Technology OfficerCommented:

Maybe you want to look at the insert capabilities of XML.modify() in SQL Server.
Here is an example:
SET @xml = '<node1>test1</node1><node2>test2</node2>';

SET @xml.modify('insert <node3>test3</node3> as last into (/)');

SELECT @xml;

Open in new window

Hope that helps!

Best regards and happy coding,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.