我有这样的数据:
是否可以使用该表中的SQL查询生成此XML文件作为输出?
答案 0 :(得分:3)
不确定您实际遇到了哪些问题,但需要使用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');
结果: