繁体   English   中英

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

[英]SQL Server : creating XML with child nodes for non unique values

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

我正在使用Microsoft SQL Server 2012

例如

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>节点下的帮助。

谢谢!

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

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>

测试数据

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM