troubleshooting Question

SQL Server XML Select sub tables

Avatar of Larry Brister
Larry BristerFlag for United States of America asked on
XMLMicrosoft SQL Server
4 Comments1 Solution201 ViewsLast Modified:
In my following code I am getting ONLY the top phone number for the individuals from the Phone.*

This...
SELECT TOP 10
        Employee.IndividualID ,
        Employee.FirstName ,
        Employee.LastName,
        Phone.*
FROM    Individuals AS Employee
        INNER JOIN ( SELECT IndividualID AS IndividualID,
                            Code AS CODE,
                            [Description] AS [DESCRIPTION] ,
                            PhoneNumber AS PhoneNumber
                     FROM   dbo.vwIndividualPhones        
                   ) Phone ON Phone.IndividualID = Employee.IndividualID
FOR     XML AUTO , ROOT('Employees') , ELEMENTS; 

Open in new window


Gets me this...
<Employees>
  <Employee>
    <IndividualID>1747652</IndividualID>
    <FirstName>ALICIA</FirstName>
    <LastName>SMITH</LastName>
    <Phone>
      <IndividualID>1747652</IndividualID>
      <CODE>HP</CODE>
      <DESCRIPTION>Home Phone</DESCRIPTION>
      <PhoneNumber>1111111111</PhoneNumber>
    </Phone>
  </Employee>
</Employees>

Open in new window


When I want this...

<Employees>
  <Employee>
    <IndividualID>1747652</IndividualID>
    <FirstName>ALICIA</FirstName>
    <LastName>SMITH</LastName>
<Phones>
    <Phone>
      <IndividualID>1747652</IndividualID>
      <CODE>HP</CODE>
      <DESCRIPTION>Home Phone</DESCRIPTION>
      <PhoneNumber>1111111111</PhoneNumber>
    </Phone>
    <Phone>
      <IndividualID>1747652</IndividualID>
      <CODE>WP</CODE>
      <DESCRIPTION>Work Phone</DESCRIPTION>
      <PhoneNumber>2222222222</PhoneNumber>
    </Phone>
</Phones>
  </Employee>
</EMployees>

Open in new window

ASKER CERTIFIED SOLUTION
Nakul Vachhrajani
Senior Manager

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 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 4 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