Avatar of Larry Brister
Larry Brister
Flag for United States of America asked on

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

Microsoft SQL ServerXML

Avatar of undefined
Last Comment
Ryan Chong

8/22/2022 - Mon
zc2

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 Brister

ASKER
zc2
I prefer to do everything in SQL Server
Larry Brister

ASKER
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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Ryan Chong

do you still need assistance here?

can you provide your source table structures?
Larry Brister

ASKER
I'll provide as soon as I return to my desk
Larry Brister

ASKER
The vwPhones is essentially three columns
id
typeid
and the PhoneNumber
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Ryan Chong

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question