简体   繁体   中英

Xml output in SQL Server

I want table output as XML. My table result is

在此处输入图片说明

I need result like

在此处输入图片说明

Query

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; 

Thanks,

S.Sundar

we have to write a query using GROUP BY , XML path not allowed to write sql within xml path(not allowed select statement)

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

OUTPUT

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

Well, in your particular case to achieve your goal you should made three subselects for your three subbranches Color , Transport and Electronics and then join that xml parts under xml root.

You can do it using following query:

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

Take a look at EXPLICIT Mode with FOR XML MSDN . It can be a pain , but you can control the output ;-)

Finally I put code like this

Select Category as value , (SELECT Attribute as value from @colorTable where Category= a.Category
FOR XML raw('attribute'), TYPE) from @colorTable as a group by Category FOR XML raw('category'), ROOT ('xml'), type;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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