【问题标题】:Xml output in SQL ServerSQL Server 中的 XML 输出
【发布时间】:2015-11-28 03:45:54
【问题描述】:

我希望表格输出为 XML。我的表格结果是

我需要这样的结果

查询

Declare @colorTable table (Category varchar(100),Attribute varchar(100))
insert into @colorTable values ('Color','Red')
insert into @colorTable values ('Color','Blue')
insert into @colorTable values ('Color','Green')
insert into @colorTable values ('Transport','Bus')
insert into @colorTable values ('Transport','Car')
insert into @colorTable values ('Transport','Twoweeler')

select * from @colorTable
FOR XML PAth(''), ROOT ('xml'), ELEMENTS; 

谢谢,

S.桑达

【问题讨论】:

  • 到目前为止你有没有尝试过?例如,使用select ... for xml
  • 是的,我试过了。没有得到我预期的结果
  • 如果您将展示您的一些尝试,那么有人可能会指出您所犯的错误。否则它看起来像“请为我写代码”请求。
  • 我试过这样Declare @colorTable table (Category varchar(100),Attribute varchar(100)) insert into @colorTable values ('Color','Red') insert into @colorTable values ('Color','Blue') insert into @colorTable values ('Color','Green') insert into @colorTable values ('Transport','Bus') insert into @colorTable values ('Transport','Car') insert into @colorTable values ('Transport','Twoweeler') select * from @colorTable FOR XML PAth(''), ROOT ('xml'), ELEMENTS;

标签: sql sql-server xml sql-server-2008 stored-procedures


【解决方案1】:

我们必须使用GROUP BY编写查询,XML路径不允许在xml path(not allowed select statement)中写入sql

Declare @colorTable table (Category varchar(100),Attribute varchar(100))
insert into @colorTable values ('Color','Red')
insert into @colorTable values ('Color','Blue')
insert into @colorTable values ('Color','Green')
insert into @colorTable values ('Transport','Bus')
insert into @colorTable values ('Transport','Car')
insert into @colorTable values ('Transport','Twoweeler')

select T1.Category as '@Value',
       (
       select T2.Attribute as '@Value'
       from @colorTable as T2
       where T2.Category = T1.Category
       group by T2.Attribute
       for xml path('Attribute'), type
       )
from @colorTable as T1
group by Category
for xml path('Category'), root('xml')

输出

<xml>
  <Category Value="Color">
    <Attribute Value="Blue" />
    <Attribute Value="Green" />
    <Attribute Value="Red" />
  </Category>
  <Category Value="Transport">
    <Attribute Value="Bus" />
    <Attribute Value="Car" />
    <Attribute Value="Twoweeler" />
  </Category>
</xml>

【讨论】:

    【解决方案2】:

    好吧,在您的特定情况下,为了实现您的目标,您应该为您的三个子分支 ColorTransportElectronics 进行三个子选择,然后在 xml 根目录下加入该 xml 部分。

    您可以使用以下查询:

    select
        (
            select
                Attribute as 'attribute/@value'
            from @colorTable
            where Category = 'Color'
            for xml path(''), type
        ) as Color,
        (
            select
                Attribute as 'attribute/@value'
            from @colorTable
            where Category = 'Transports'
            for xml path(''), type
        ) as Transports,
        (
            select
                Attribute as 'attribute/@value'
            from @colorTable
            where Category = 'Electronics'
            for xml path(''), type
        ) as Electronics
    for xml path(''), root('xml')
    

    【讨论】:

    • color ,传输将动态进行。那不是静态的
    • @user2354274 在您的 xml 中使用动态元素名称似乎是个坏主意。因此,如果它应该是动态的,请考虑将您的 xml 结构从 &lt;Color&gt;...&lt;/Color&gt; 更改为 &lt;Category name="Color"&gt;...&lt;/Category&gt;
    【解决方案3】:

    看看EXPLICIT Mode with FOR XMLMSDN。这可能很痛苦,但您可以控制输出;-)

    【讨论】:

      【解决方案4】:

      最后我放了这样的代码

      选择类别作为值, (从 @colorTable 中选择属性作为值,其中 Category= a.Category
      FOR XML 原始('属性'), 类型) 来自@colorTable 按类别分组 FOR XML raw('category'), ROOT ('xml'), type;

      【讨论】:

        猜你喜欢
        • 2010-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-19
        相关资源
        最近更新 更多