SQL Server:使用子节点为非唯一值创建XML

时间:2014-06-02 18:09:18

标签: sql sql-server xml sql-server-2012

我很难弄清楚如何从具有子节点的SQL select创建XML文件来存储非唯一值。

我正在使用Microsoft SQL Server 2012

e.g。

select company, founder
from companies

查询结果为:

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

我想生成类似

的XML
<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>

我不确定节点<founders>是否必需,我认为将创始人直接置于<values>节点之下也可以,因为他们保留在合适的公司,获得id有一个列表。

我现在使用FOR XML获得了什么,然后我尝试了以下不同的选项:

<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>

与我当时需要达到的目标不相容。

非常感谢任何有助于让所有创始人置于同一<values>节点下的帮助。

谢谢!

2 个答案:

答案 0 :(得分:3)

搜索了一下互联网并找到了解决方案:

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')

结果:

<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>

答案 1 :(得分:1)

测试数据

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')

<强>查询

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

<强>输出

<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>