以不同的方式在Sql Server中生成XML文件

时间:2014-12-16 13:54:14

标签: sql-server xml split

我创建了两个表Employer和ReportPage。

EmployerId是两个表的公共字段。 我想从表中生成XML。我写了一些查询并执行。

查询在这里:

select e.EmployerName as EmployerName, 
       e.EmployerId as EmployerId, (
select
      rp.EmployerId as EmployerId,
      rp.PageNumber as PageNumber, 
      rp.Title as Title, 
      rp.Score as Score
       from ReportPage rp  where rp.EmployerId = e.EmployerId
       FOR XML AUTO, TYPE, ELEMENTS)
       from Employer e where EmployerId = 09340 FOR XML AUTO, TYPE, ELEMENTS, root
      ;

它提供了如下输出XML:

<?xml version = '1.0' encoding = 'utf-8' standalone = 'no'?>
    <root>
 <e>
    <EmployerName>Company, Inc.</EmployerName>
    <EmployerId>09340</GroupNumber>
      <rp>
          <EmployerId>09340</EmployerId>
          <PageNumber>1</PageNumber>
          <Title>Executive Summary</Title>    
          <Score>67</Score>
      </rp>  
 </e>   
 <e>
    <EmployerName>Company, Inc.</EmployerName>
    <EmployerId>09340</GroupNumber>
      <rp>
          <EmployerId>09340</EmployerId>
          <PageNumber>2</PageNumber>
          <Title>Executive Summary</Title>    
          <Score>75</Score>
      </rp>  
 </e>    
 <e>
    <EmployerName>Company, Inc.</EmployerName>
    <EmployerId>09340</GroupNumber>
      <rp>
          <EmployerId>09340</EmployerId>
          <PageNumber>3</PageNumber>
          <Title>Executive Summary</Title>    
          <Score>80</Score>
      </rp>  
 </e>
    </root>

但我期待这种形式的XML输出:

<?xml version = '1.0' encoding = 'utf-8' standalone = 'no'?>
    <root>
       <e>
          <EmployerName>Company, Inc.</EmployerName>
          <EmployerId>09340</GroupNumber>
       </e>
      <rp>
<EmployerId>09340</EmployerId>
          <PageNumber>1</PageNumber>
          <Title>Executive Summary</Title>    
          <Score>67</Score>
      </rp>     
      <rp>
<EmployerId>09340</EmployerId>
          <PageNumber>2</PageNumber>
          <Title>Common</Title>   
          <Score>75</Score>
      </rp>     
      <rp>
<EmployerId>09340</EmployerId>
          <PageNumber>3</PageNumber>
          <Title>Physical</Title>     
          <Score>80</Score>
      </rp>   
    </root>

能帮助我吗?我已经尝试了很长时间。谢谢!

1 个答案:

答案 0 :(得分:1)

应用文章&#34; belle’s sql musings: SQLXML: How to Join Multiple XML Snippets (using query() and UNION ALL)&#34;你的问题给了我这个代码:

DECLARE @EmployerId VARCHAR(5) = '09340'

DECLARE @eQuery XML
DECLARE @rpQuery XML

SET @eQuery = (
  SELECT
    e.EmployerName AS EmployerName, 
    e.EmployerId AS EmployerId
  FROM
    Employer e
  WHERE
    EmployerId = @EmployerId
  FOR XML AUTO, TYPE, ELEMENTS, ROOT
)

SET @rpQuery = (
  SELECT
    rp.PageNumber AS PageNumber, 
    rp.Title AS Title, 
    rp.Score AS Score
  FROM
    ReportPage rp
  WHERE
    rp.EmployerId = @EmployerId
  ORDER BY
    rp.PageNumber
  FOR XML AUTO, TYPE, ELEMENTS, ROOT
)

SELECT
  @eQuery.query('//e')
UNION ALL
SELECT
  @rpQuery.query('//rp')
FOR XML PATH (''), ROOT('root'), TYPE

您可以在SQL小提琴中在线查看结果:http://sqlfiddle.com/#!6/e95b5/7/0


免责声明:我不是T-SQL专家,查询可能会有所改进,一些神奇的关键词被抛弃,我只是使用谷歌一起攻击它而不知道它真正做了什么(以及为什么)