SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Cross Apply to get child parent value from Xml in SQL Server


Cross Apply to get child parent value from Xml in SQL Server

Author
Message
Paresh Dehadray
Paresh Dehadray
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 28
I have following xml:
<root>
<row value="US">
<col value="00">Jon</col>
<col value="01">David</col>
<col value="02">Mike</col>
<col value="03">Nil</col>
</row>
<row value="Canada">
<col value="C1">Pollard</col>
</row>
<row value="Japan">
<col value="J1">Yin</col>
<col value="J2">Li</col>
</row>
<row value="India">
<col value="MP">Ram</col>
<col value="UP">Paresh</col>
<col value="AP">Mohan</col>
</row>
</root>


and I want following output by using SQL Server query:

US 00 Jon
US 01 David
US 02 Mike
US 03 Nil
Canada C1 Pollard
Japan J1 Yin
Japan J2 Li
India MP Ram
India UP Paresh
India AP Mohan


I am using following SQL query:


declare @x xml
set @x =
'<root>
<row value="US">
<col value="00">Jon</col>
<col value="01">David</col>
<col value="02">Mike</col>
<col value="03">Nil</col>
</row>
<row value="Canada">
<col value="C1">Pollard</col>
</row>
<row value="Japan">
<col value="J1">Yin</col>
<col value="J2">Li</col>
</row>
<row value="India">
<col value="MP">Ram</col>
<col value="UP">Paresh</col>
<col value="AP">Mohan</col>
</row>
</root>'

select r.value('@value','varchar(100)'),r.value('.','varchar(100)')
from @x.nodes('root') as m(c)
cross apply m.c.nodes('row/col') as x(r)


I am unable to get first column which contains the value of parent row. Can you please suggest what changes I can make to get first column value?

Thanks in advance

Paresh
kupy
kupy
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1088 Visits: 2655
Hi,

I was just paling around with the query and got this:

declare @x xml
set @x =
'<root>
<row value="US">
<col value="00">Jon</col>
<col value="01">David</col>
<col value="02">Mike</col>
<col value="03">Nil</col>
</row>
<row value="Canada">
<col value="C1">Pollard</col>
</row>
<row value="Japan">
<col value="J1">Yin</col>
<col value="J2">Li</col>
</row>
<row value="India">
<col value="MP">Ram</col>
<col value="UP">Paresh</col>
<col value="AP">Mohan</col>
</row>
</root>'

select v.value('../@value','varchar(100)'), v.value('@value','varchar(100)'), v.value('.[1]','varchar(100)')
from @x.nodes('root') as m(c)
cross apply m.c.nodes('row/col') as z(v)


Paresh Dehadray
Paresh Dehadray
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 28
Thanks it is working. I also got below another way:

select c.value('(../@value)[1]', 'varchar(100)'),c.value('@value','varchar(100)'), c.value('.','varchar(100)')
from @x.nodes('/root/row/col') as m(c)

Thanks
Paresh
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search