Link to home
Start Free TrialLog in
Avatar of b_levitt
b_levitt

asked on

XQuery to return subnodes

Consider the following xml documents in an xml column:

row 1:
<doc>
  <children>
    <value>1</value>
    <value>2</value>
  </children>
</doc>

row 2:
<doc>
  <children>
    <value>3</value>
    <value>4</value>
    <value>5</value>
  </children>
</doc>

I'd like an xquery statement (xml.value()?) to return a rowset of:
1
2
3
4
5
ASKER CERTIFIED SOLUTION
Avatar of chapmandew
chapmandew
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
here we go:
set ansi_padding on
 
declare @t  table ( data xml )
insert into @t ( data ) values ('
<doc>
  <children>
    <value>1</value>
    <value>2</value>
  </children>
</doc>' )
 
insert into @t ( data ) values ('<doc>
  <children>
    <value>3</value>
    <value>4</value>
    <value>5</value>
  </children>
</doc>' )
 
select t.data , x.v.value('.', 'int')
  from @t t
  cross apply t.data.nodes('/doc/children/value') x (v)

Open in new window

And we can only hope the user is using at least SQL Server 2005. :)
Oops, never mind, I need to start reading the question through the end. :)
Avatar of b_levitt
b_levitt

ASKER

I've said it before and I'll say it again - the SQL Server experts alone are worth a paid membership to EE!  I'll give it a shot in the morning and award points.
Works great chap.  Sorry you lost the race angel.