您的子查询不能返回两个值。如果只想连接字符串,则根本不需要
xml数据类型。您可以
stuff()在单个语句中执行and子查询:
declare @Rep1Names nvarchar(max) = ( stuff((select ', [' + report_name + ']' as namefrom (select distinct report_order, report_name from #report ) xorder by report_orderfor xml path('') ) ), 1, 1, '');declare @Rep2Names nvarchar(max) = ( stuff(select ', isnull([' + report_name + '], 0) as [' + report_name + ']' as resfrom (select distinct report_order, report_name from #report ) xorder by report_orderfor xml path('') ) ), 1, 1, '');


