表样:
DECLARE @STR VARCHAR(8000)
SELECT @STR=ISNULL(@STR+',','')+userID FROM (SELECT DISTINCT userID FROM Users)AS T
SELECT @STR AS NAME
结果:
declare @output varchar(8000)
select @output = coalesce(@output + ',' , '') + userid from Users
print @output
结果:
SELECT GROUP_CONCAT(userID) as id from Users
查询结果: