SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Adding new xml tag with a value takeen from anothe
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

winman
Starting Member

26 Posts

Posted - 03/01/2013 :  09:40:03  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3639 Posts

Posted - 03/01/2013 :  09:43:11  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 03/01/2013 :  13:08:05  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000