Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag 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

Avatar of zc2
zc2
Flag of United States of America image

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.
Avatar of Larry Brister

ASKER

zc2
I prefer to do everything in SQL Server
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

do you still need assistance here?

can you provide your source table structures?
I'll provide as soon as I return to my desk
The vwPhones is essentially three columns
id
typeid
and the PhoneNumber
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial