所有 Id 都放在一个列中

All Id into a single column

select 来自产品的 ProductId 我得到如下输出: 产品编号

  1
  2
  3
  4

我想得到如下输出:

产品编号

1,2,3,4

试试这个:

declare @aa varchar (200)
set @aa = ''

select @aa = 
case when @aa = ''
then cast(Productid as varchar)
else @aa + coalesce(',' + cast(Productid as varchar), '')
end
from Products

print @aa