Attempting to write a SQL Server 2008 XML query. I have simplified my example here, but with all this data:

create table #parentinfo (name varchar(50), city varchar(50), state varchar(50))
insert #parentinfo values ('joe', 'yonkers', 'ny')
insert #parentinfo values ('sue', 'sacremento', 'ca')

-- parentname is foreign key to name field in #parentinfo
create table #childinfo (parentname varchar(50), childxml xml)
insert #childinfo values ('joe', '<child>mary</child><child>ben</child>')
insert #childinfo values ('sue', '<child>sally</child><child>roger</child>')

I have to output XML that appears such as this:

<parentinfo>
<parent>
    <name>joe</name>
    <city>yonkers</city>
    <state>ny</state>
    <children>
        <child>mary</child>
        <child>ben</child>
    </children>
</parent>
<parent>
    <name>sue</name>
    <city>sacremento</city>
    <state>ca</state>
    <children>
        <child>sally</child>
        <child>roger</child>
    </children>
</parent>
</parentinfo>

This question does that:

select  name,
    city,
    state, 
    (select childxml
    from #childinfo c
    where  c.parentname = p.name
    --for xml auto, type, elements
    ) as 'children'
from #parentinfo p
group by name, city, state
for xml path('parent')

However, I've problems when among the parents comes with an extra row in childinfo table. Add this row towards the table:

insert #childinfo values ('joe', '<child>chucky</child><child>pebbles</child>')

And my query fills up stating that subquery returns a lot more than 1 value. I have to handle this. Does anybody understand how to resolve?

Thanks.

select  name,
    city,
    state, 
    (select childxml as '*'
     from #childinfo c
     where  c.parentname = p.name
     for xml path(''), type
    ) as 'children'
from #parentinfo p
group by name, city, state
for xml path('parent')

Result:

<parent>
  <name>joe</name>
  <city>yonkers</city>
  <state>ny</state>
  <children>
    <child>mary</child>
    <child>ben</child>
    <child>chucky</child>
    <child>pebbles</child>
  </children>
</parent>
<parent>
  <name>sue</name>
  <city>sacremento</city>
  <state>ca</state>
  <children>
    <child>sally</child>
    <child>roger</child>
  </children>
</parent>

Should you have had a "regular" child table - with only a reputation for the kid - and never an XML fragment, you could utilize this question to obtain precisely what you are searching for:

create table #parentinfo (name varchar(50), city varchar(50), state varchar(50))
insert #parentinfo values ('joe', 'yonkers', 'ny')
insert #parentinfo values ('sue', 'sacremento', 'ca')

-- parentname is foreign key to name field in #parentinfo
create table #childinfo (parentname varchar(50), childname VARCHAR(50))
insert #childinfo values ('joe', 'mary')
insert #childinfo values ('joe', 'ben')
insert #childinfo values ('sue', 'sally')
insert #childinfo values ('sue', 'roger')

insert #childinfo values ('joe', 'chucky')
insert #childinfo values ('joe', 'pebbles')

select  name,
    city,
    state, 
    (select childname as 'child'
     from #childinfo c
     where c.parentname = p.name
     order by childname
     for xml PATH(''), type
    ) as 'children'
from #parentinfo p
group by name, city, state
for xml path('parent'), root('parentinfo')

The resulting output is:

<parentinfo>
  <parent>
    <name>joe</name>
    <city>yonkers</city>
    <state>ny</state>
    <children>
      <child>ben</child>
      <child>chucky</child>
      <child>mary</child>
      <child>pebbles</child>
    </children>
  </parent>
  <parent>
    <name>sue</name>
    <city>sacremento</city>
    <state>ca</state>
    <children>
      <child>roger</child>
      <child>sally</child>
    </children>
  </parent>
</parentinfo>