向变量赋值的 SELECT 语句不能与数据检索操作结合使用

问题描述:

tablename表结构:
id int 标识主键
userid int

orderid int

表里的数据是这样:
id userid orderid
1 1 1000
2 1 1001
3 1 1003

4 2 1004

如何查出这样结果:
userid orderids
1 1000,1001,1003
2 1004

我使用SQL变量 :

Declare @tmp varchar(200) set @tmp=''
select userid, @tmp=@tmp+','+ convert(varchar,orderid) as orderids from tablename group by userid

select @tmp

但提示 :
向变量赋值的 SELECT 语句不能与数据检索操作结合使用

急………………