MS SQL Server XML Output

Larry Brister
Larry Brister used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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. Developer

Author

Commented:
zc2
I prefer to do everything in SQL Server
Larry Bristersr. Developer

Author

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 you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Ryan ChongSoftware Team Lead

Commented:
do you still need assistance here?

can you provide your source table structures?
Larry Bristersr. Developer

Author

Commented:
I'll provide as soon as I return to my desk
Larry Bristersr. Developer

Author

Commented:
The vwPhones is essentially three columns
id
typeid
and the PhoneNumber
Software Team Lead
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial