我有这样的数据:
是否可以使用该表中的 SQL 查询生成此 XML 文件作为输出?
最佳答案
不确定您实际上遇到了什么问题,但您需要使用 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');
结果:
关于SQL Server 表数据到带有列值的 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47451246/