Solved

Adding node to XML data using Xquery

Posted on 2011-09-19
1
342 Views
Last Modified: 2012-05-12
Is there a way to add a node to an XML string using Xquery?

For example:

<node1>test1</node1>
<node2>test2</node2>

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.

0
Comment
Question by:felkamau
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36563757
felkamau,

Maybe you want to look at the insert capabilities of XML.modify() in SQL Server.
Here is an example:
DECLARE @xml XML;
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,

Kevin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question