MS SQL Server XML Output

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>

Open in new window




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;

Open in new window

Larry Bristersr. DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zc2Commented:
I would do that in two steps. First, creating a simple XML by adding "for XML auto" to your SQL expression.
Then use XSLT to transform it to whatever schema you like.
Larry Bristersr. DeveloperAuthor Commented:
zc2
I prefer to do everything in SQL Server
Larry Bristersr. DeveloperAuthor Commented:
I am getting xml this way...
But I need all of the Phones wrapped in its own <Phones> element


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

Open in new window


This is the output...
<Employees>
  <Employee>
    <IndividualID>1747652</IndividualID>
    <FirstName>ALICIA</FirstName>
    <LastName>GOODGIRL</LastName>
    <Phone>
      <Code>AP</Code>
      <Description>Alt Phone</Description>
      <PhoneNumber>0</PhoneNumber>
    </Phone>
    <Phone>
      <Code>FAX</Code>
      <Description>Fax</Description>
      <PhoneNumber>0</PhoneNumber>
    </Phone>
    <Phone>
      <Code>CP</Code>
      <Description>Cell Phone</Description>
      <PhoneNumber>1234123412</PhoneNumber>
    </Phone>
    <Phone>
      <Code>HP</Code>
      <Description>Home Phone</Description>
      <PhoneNumber>9999999999</PhoneNumber>
    </Phone>
    <Phone>
      <Code>CP</Code>
      <Description>Cell Phone</Description>
      <PhoneNumber>8888888888</PhoneNumber>
    </Phone>
    <Phone>
      <Code>WP</Code>
      <Description>Work Phone</Description>
      <PhoneNumber>7777777777</PhoneNumber>
    </Phone>
  </Employee>
</Employees>

Open in new window

Ensure Business Longevity with As-A-Service

Using the as-a-service approach for your business model allows you to grow your revenue stream with new practice areas, without forcing you to part ways with existing clients just because they don’t fit the mold of your new service offerings.

Ryan ChongSoftware Team LeadCommented:
do you still need assistance here?

can you provide your source table structures?
Larry Bristersr. DeveloperAuthor Commented:
I'll provide as soon as I return to my desk
Larry Bristersr. DeveloperAuthor Commented:
The vwPhones is essentially three columns
id
typeid
and the PhoneNumber
Ryan ChongSoftware Team LeadCommented:
for a quick fix, you may amend the part of:

(SELECT Phone.Code ,
        Phone.[Description] ,
        Phone.PhoneNumber FROM dbo.vwIndividualPhones Phone WHERE Phone.IndividualID = Employee.IndividualID
		FOR XML AUTO, TYPE, ELEMENTS)

Open in new window


to:

CAST('<Phones>'+CAST((SELECT Phone.Code ,
        Phone.[Description] ,
        Phone.PhoneNumber FROM dbo.vwIndividualPhones Phone WHERE Phone.IndividualID = Employee.IndividualID
		FOR XML AUTO, TYPE, ELEMENTS) AS VARCHAR(MAX))+'</Phones>' AS XML)

Open in new window


a complete test (simplified ver) is shown as follows
declare @Employee table
(
	IndividualID int,
	FirstName varchar(80),
	LastName varchar(80)
)
insert into @Employee
values
(123, 'Larry', 'Brister'),
(124, 'Bob', 'Carrr'),
(1747652, 'ALICIA', 'GOODGIRL');

declare @vwIndividualPhones table
(
	IndividualID int,
	Code varchar(20),
	Description varchar(100),
	PhoneNumber varchar(20)
)
insert into @vwIndividualPhones
values
(123, 'FAX', 'Fax', '1234569874'),
(123, 'WRK', 'Work', '9541234567'),
(124, 'FAX', 'Fax', '1234599874'),
(124, 'WRK', 'Work', '2222222222'),
(1747652, 'AP', 'Alt Phone', '0'),
(1747652, 'FAX', 'Fax', '0'),
(1747652, 'CP', 'Cell Phone', '1234123412'),
(1747652, 'CP', 'Cell Phone', '1234123412'),
(1747652, 'HP', 'Home Phone', '9999999999'),
(1747652, 'CP', 'Cell Phone', '8888888888');


SELECT TOP 1
        Employee.IndividualID ,
        Employee.FirstName ,
        Employee.LastName ,
        CAST('<Phones>'+CAST((SELECT Phone.Code ,
        Phone.[Description] ,
        Phone.PhoneNumber FROM @vwIndividualPhones Phone WHERE Phone.IndividualID = Employee.IndividualID
		FOR XML AUTO, TYPE, ELEMENTS) AS VARCHAR(MAX))+'</Phones>' AS XML)
FROM    @Employee AS Employee

        INNER JOIN @vwIndividualPhones Phone ON Phone.IndividualID = Employee.IndividualID
FOR     XML AUTO , ROOT('Employees') , ELEMENTS; 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.