Since you're using SQL Server, you can do this:
declare @list nvarchar(max)
select @list = ''
select
@list = @list +
case when @list = '' then userid
else ',' + userid end
from
UsersInAuthentication
set @list = '(' + substring(@list, 1, 999) + ')'
print @list
This is a nifty trick that builds the variable by appending each row's value to the current value of the variable. It's rather handy with table operations, and the like.
For posterity:
In MySQL, you can uses group_concat
:
select
concat('(' + group_concat(userid order by userid separator ',') + ')')
from
UserInAuthentication
In Oracle, you can use a cursor:
cursor c_users is
select userid from usersinauthentication;
out_users varchar2(4000);
begin
for r_user in c_users loop
if out_users is null then
out_users:= r_user.first_Name;
else
out_users:= out_users ||', '|| r_user.first_Name;
end if;
end loop;
return out_users;