Hi,
Say I have this setup:
-- tables
declare @main table (id int, name varchar(20))
declare @subA table (id int, mid int, name varchar(20))
declare @subA1 table (id int, subAid int, name varchar(20))
declare @subA2 table (id int, subAid int, name varchar(20))
declare @subB table (id int, mid int, name varchar(20))
-- sample data
insert @main values (1, 'A')
insert @main values (2, 'B')
insert @SubA values (1, 1, 'A')
insert @SubA values (2, 1, 'B')
insert @SubA values (3, 2, 'C')
insert @SubA1 values (1, 1, 'A')
insert @SubA2 values (1, 2, 'A')
insert @SubB values (1, 1, 'A')
insert @SubB values (2, 1, 'B')
insert @SubB values (3, 2, 'C')
-- results
select m.id, m.name, a.name, a1.name, a2.name, b.name
from @main m
left outer join @SubA a on m.id = a.mid
left outer join @SubA1 a1 on a.id = a1.subAid
left outer join @SubA2 a2 on a.id = a2.subAid
left outer join @SubB b on m.id = b.mid
This returns:
1 A A A NULL A
1 A A A NULL B
1 A B NULL A A
1 A B NULL A B
2 B C NULL NULL C
If I use "for xml auto" then I get:
<m id="1" name="A">
<a name="A">
<a1 name="A">
<a2>
<b name="A" />
<b name="B" />
</a2>
</a1>
</a>
<a name="B">
<a1>
<a2 name="A">
<b name="A" />
<b name="B" />
</a2>
</a1>
</a>
</m>
<m id="2" name="B">
<a name="C">
<a1>
<a2>
<b name="C" />
</a2>
</a1>
</a>
</m>
However, this isn't what I need. What I want to show is that @main is the main table which has two children: @subA and @SubB. @SubA in turn also has two children: @SubA1 and @SubA2, so I would like to get back:
<m id="1" name="A">
<a name="A">
<a1 name="A"></a1>
<a2></a2>
</a>
<a name="B">
<a1></a1>
<a2 name="A"></a2>
</a>
<b name="A" />
<b name="B" />
</m>
<m id="2" name="B">
<a name="C">
<a1></a1>
<a2></a2>
</a>
<b name="C" />
</m>
I'm pretty sure that I will have to use "for xml explicit", but out of all the attempts I have tried so far I haven't been able to get the format that I need.
Can anyone show an example query that will return the data in the required format?
Thanks, Mark