【问题标题】:SQL Server : creating XML with child nodes for non unique valuesSQL Server:为非唯一值创建带有子节点的 XML
【发布时间】:2014-07-22 22:26:28
【问题描述】:

我很难弄清楚如何从 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>

我不确定节点&lt;founders&gt; 是必需的,我认为让创始人直接在&lt;values&gt; 节点下也可以正常工作,因为他们留在正确的公司,获得一个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>

这与我需要实现的目标不兼容。

非常感谢任何帮助将所有创始人置于同一 &lt;values&gt; 节点下。

谢谢!

【问题讨论】:

  • 刚刚更新很抱歉

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


【解决方案1】:

上网搜索了一下,找到了解决办法:

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>

【讨论】:

    【解决方案2】:

    测试数据

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多