Din underforespørgsel kan ikke returnere to værdier. Hvis du blot ønsker at sammenkæde strenge, behøver du ikke xml
datatype overhovedet. Du kan gøre stuff()
og underforespørgsel i en enkelt sætning:
declare @Rep1Names nvarchar(max) = (
stuff((select ', [' + report_name + ']' as name
from (select distinct report_order, report_name
from #report
) x
order by report_order
for xml path('')
)
), 1, 1, '');
declare @Rep2Names nvarchar(max) = (
stuff(select ', isnull([' + report_name + '], 0) as [' + report_name + ']' as res
from (select distinct report_order, report_name
from #report
) x
order by report_order
for xml path('')
)
), 1, 1, '');