What syntax is required to have multiple elements generated to XML? Example 1 table of people relates to a table of phone numbers. The phone numbers table can have different types... home, cell, spouse...etc.
This is what I have MS SQL2008:
select
p1.id as '@id',
rtrim(p1.first) 'first',
rtrim(p1.last) 'last',
p2.id 'Phone/id',
rtrim(p2.phonetype) 'Phone/phonetype',
rtrim(p2.phonenumber)'Phone/phonenumber'
from person p1
left outer join phone p2 on p1.id = p2.personid
order by p1.last
for xml path('Person'),root ('People')
The output below has 2 of the same persons.. 1 for each related phone type.
How can I get phone element to repeat with the different related phone records?
<People>
<Person id="1">
<first>Tom</first>
<last>Wilson</last>
<Phone>
<id>1</id>
<phonetype>HOME</phonetype>
<phonenumber>(111)222-3333</phonenumber>
</Phone>
</Person>
<Person id="1">
<first>Tom</first>
<last>Wilson</last>
<Phone>
<id>2</id>
<phonetype>CELL</phonetype>
<phonenumber>(111)333-4444</phonenumber>
</Phone>
</Person>
</People>
Thanks in advance for any direction...
This is what I have MS SQL2008:
select
p1.id as '@id',
rtrim(p1.first) 'first',
rtrim(p1.last) 'last',
p2.id 'Phone/id',
rtrim(p2.phonetype) 'Phone/phonetype',
rtrim(p2.phonenumber)'Phone/phonenumber'
from person p1
left outer join phone p2 on p1.id = p2.personid
order by p1.last
for xml path('Person'),root ('People')
The output below has 2 of the same persons.. 1 for each related phone type.
How can I get phone element to repeat with the different related phone records?
<People>
<Person id="1">
<first>Tom</first>
<last>Wilson</last>
<Phone>
<id>1</id>
<phonetype>HOME</phonetype>
<phonenumber>(111)222-3333</phonenumber>
</Phone>
</Person>
<Person id="1">
<first>Tom</first>
<last>Wilson</last>
<Phone>
<id>2</id>
<phonetype>CELL</phonetype>
<phonenumber>(111)333-4444</phonenumber>
</Phone>
</Person>
</People>
Thanks in advance for any direction...