troubleshooting Question

MS SQL Server XML Output

Avatar of Larry Brister
Larry BristerFlag for United States of America asked on
Microsoft SQL ServerXML
7 Comments1 Solution210 ViewsLast Modified:
In my select below what I would like to produce is this XML
<Individuals>
  <Individual>
    <IndividualID> 123</IndividualID>
    <FirstName>Larry</FirstName>
    <LastName>Brister</LastName>
    <Phones>
      <Phone>
        <code>FAX</code>
        <phonenumber>1234569874</phonenumber>
      </Phone>
      <Phone>
        <code>WRK</code>
        <phonenumber>9541234567</phonenumber>
      </Phone>
    </Phones>
  </Individual>
  <Individual>
    <IndividualID>124</IndividualID>
    <FirstName>Bob</FirstName>
    <LastName>Carrr</LastName>
    <Phones>
      <Phone>
        <code>FAX</code>
        <phonenumber>1234599874</phonenumber>
      </Phone>
      <Phone>
        <code>WRK</code>
        <phonenumber>2222222222</phonenumber>
      </Phone>
    </Phones>
  </Individual>
</Individuals>



SELECT  i.IndividualID ,
        i.FirstName ,
        i.LastName ,
        e.EmailAddress ,
        pp.Code ,
        pp.PhoneNumber
FROM    Individuals i
        JOIN dbo.IndividualEmails e ON i.IndividualID = e.IndividualID
        JOIN ( SELECT   i.IndividualID ,
                        i.PhoneNumber ,
                        t.Code
               FROM     IndividualPhoneNumbers i
                        JOIN dbo.PhoneNumberTypes t ON t.PhoneNumberTypeID = i.PhoneNumberTypeID
             ) pp ON i.IndividualID = pp.IndividualID;
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros