1

I am having a hard time figuring out how to create an XML file from a SQL select that has sub nodes to store the not unique values.

I am using Microsoft SQL Server 2012

e.g.

select company, founder
from companies

The result of the query is:

 microsoft corp       Bill Gates
 microsoft corp       Paul Allen
 apple inc            Steve Jobs
 apple inc            Ronald Wayne
 apple inc            Steve Wozniak

I would like to generate an XML like

<values>
       <company>microsoft corp</company>
       <founders>
           <founder id="1">Bill Gates</founder>
           <founder id="2">Paul Allen</founder>
       </founders>
</values>
<values>
       <company>apple inc</company>
       <founders>
           <founder id="1">Steve Jobs</founder>
           <founder id="2">Ronald Wayne</founder>
           <founder id="3">Steve Wozniak</founder>
        </founders>
       </company>
</values>

I am not sure the node <founders> is required, I think it would also work fine for me to have the founders directly under the <values> node, since they keep in the right company, getting an id to have a list.

What I get right now, using FOR XML, then I tried different options is the following:

<values>
       <company>apple inc</company>
       <founder>Steve Jobs</founder>
</values>
<values>
       <company>apple inc</company>
       <founder>Ronald Wayne</founder>
</values>
<values>
       <company>apple inc</company>
       <founder>Steve Wozniak</founder>
</values>

which is not compatible with what I need to achieve then.

Any help to get all the founders under the same <values> node is highly appreciated.

Thanks!

1
  • just updated sorry about that Commented Jun 2, 2014 at 18:18

2 Answers 2

3

Searched a bit internet and found solution:

DECLARE @table TABLE (company VARCHAR(100), founder VARCHAR(100))

 insert into @table SELECT 'microsoft corp', 'Bill Gates'
 insert into @table SELECT 'microsoft corp', 'Paul Allen'
 insert into @table SELECT 'apple inc', 'Steve Jobs'
 insert into @table SELECT 'apple inc', 'Ronald Wayne'
 insert into @table SELECT 'apple inc', 'Steve Wozniak'


 SELECT company, 
    (
        SELECT ROW_NUMBER()OVER( ORDER BY founder ) as 'founder/@id',
            founder as 'founder'
        FROM @table as F
        WHERE F.company = Comp.company
        FOR XML PATH(''), TYPE
    ) AS founders
 FROM @table as Comp
 GROUP BY company
 FOR XML PATH('VALUES')

Result:

<VALUES>
  <company>apple inc</company>
  <founders>
    <founder id="1">Ronald Wayne</founder>
    <founder id="2">Steve Jobs</founder>
    <founder id="3">Steve Wozniak</founder>
  </founders>
</VALUES>
<VALUES>
  <company>microsoft corp</company>
  <founders>
    <founder id="1">Bill Gates</founder>
    <founder id="2">Paul Allen</founder>
  </founders>
</VALUES>
Sign up to request clarification or add additional context in comments.

Comments

1

Test Data

DECLARE @Companies TABLE (company VARCHAR(100), founder VARCHAR(100))
INSERT INTO @Companies VALUES
 ('microsoft corp',       'Bill Gates'),
 ('microsoft corp',       'Paul Allen'),
 ('apple inc',            'Steve Jobs'),
 ('apple inc',            'Ronald Wayne'),
 ('apple inc',            'Steve Wozniak')

Query

SELECT company
       ,(SELECT ROW_NUMBER() OVER (PARTITION BY Company ORDER BY Founder ASC)  AS [@ID] 
               ,B.founder [text()]
         FROM @companies B
        WHERE B.company = A.company
        FOR XML PATH('Founder'),TYPE) AS Founders
 FROM  @companies A
GROUP BY A.Company       
FOR XML PATH('values'),ROOT('Doc'), ELEMENTS

OUTPUT

<Doc>
  <values>
    <company>apple inc</company>
    <Founders>
      <Founder ID="1">Ronald Wayne</Founder>
      <Founder ID="2">Steve Jobs</Founder>
      <Founder ID="3">Steve Wozniak</Founder>
    </Founders>
  </values>
  <values>
    <company>microsoft corp</company>
    <Founders>
      <Founder ID="1">Bill Gates</Founder>
      <Founder ID="2">Paul Allen</Founder>
    </Founders>
  </values>
</Doc>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.