简体   繁体   English

SQL Server表数据到具有列值的xml文件

[英]SQL Server table data to xml file with column value

I have this kind of data: 我有这种数据:

数据

Is it possible to produce this XML file as output with a SQL query from that table? 是否可以使用该表中的SQL查询将该XML文件生成为输出?

XML

Not sure what issues you actually had but you need to use for xml path() and nest them in a couple of sub-queries. 不知道您实际遇到了什么问题,但是您需要使用for xml path()并将它们嵌套在几个子查询中。

declare @T table(code varchar(30), amount money);

insert into @T(code, amount) values
('totalPAS', 389),
('sub270', 0),
('sub770', 0),
('sub270', 0);

select 'datfile' as '@objectCode', 
       (
       select T.code as '@instanceCode',
              'data_t' as '@objectCode',
              (
              select 'code' as 'ColumnValue/@name',
                     T.code as 'ColumnValue',
                     null,
                     'amount' as 'ColumnValue/@name',
                     T.amount as 'ColumnValue'
             for xml path('CustomInformation'), type
             ) 
       from @T as T
       for xml path('instance'), type
       )
for xml path('customObjectInstances');

Result: 结果:

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

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