Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Adding new xml tag with a value takeen from anothe

Author  Topic 

winman
Starting Member

26 Posts

Posted - 2013-03-01 : 09:40:03
I have a table with xml column named xmlvalue.let xml value in those column are like shown below

<z>
<a>
<b>1</b>
<c>2</c>
</a>
<a>
<b>4</b>
<c>5</c>
.
.
.
so many tags similar to above
.
.
</a>
</z>
I want to add a new node called <new>here value should betaken from another column</new> inside the <z><a> for every row in that column with <new> tag having a value which is taken from another column "abc" for that row.(column abc is not a xml column) which is in same table.How can i do it using xml query or sql query?

Thank you

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-01 : 09:43:11
Can you post a sample input XML and the corresponding desired output XML (both as well-formed XML, so someone can copy it and use it in code). That is much easier to understand than word descriptions. Make the sample data representative enough, so it covers the variations that you would have in your real data.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-01 : 13:08:05
do you mean this?


declare @test table
(
id int,
varval varchar(100),
x xml
)

insert @test
select 1,'abc',
'<z>
<a>
<b>1</b>
<c>2</c>
</a>
<a>
<b>4</b>
<c>5</c>
</a>
</z>' union all

select 2,'pqr',
'<z>
<a>
<b>21</b>
<c>22</c>
</a>
<a>
<b>14</b>
<c>52</c>
</a>
</z>'

UPDATE @test
SET x.modify('insert <new>{sql:column("varval")}</new>
into /z[1]')

SELECT * from @test


output
--------------------------------------------------------------------------------------
id varval x
--------------------------------------------------------------------------------------
1 abc <z><a><b>1</b><c>2</c></a><a><b>4</b><c>5</c></a><new>abc</new></z>
2 pqr <z><a><b>21</b><c>22</c></a><a><b>14</b><c>52</c></a><new>pqr</new></z>



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -