Insert some records into table <UserAliases>.
insert into UserAliases(UserID,Alias)
values(1,'Aliases1')
insert into UserAliases(UserID,Alias)
values(1,'Aliases2')
insert into UserAliases(UserID,Alias)
values(2,'Aliases3')
insert into UserAliases(UserID,Alias)
values(3,'Aliases4')
insert into UserAliases(UserID,Alias)
values(4,'Aliases5')
insert into UserAliases(UserID,Alias)
values(2,'Aliases6')
insert into UserAliases(UserID,Alias)
values(3,'Aliases7')
GO
CREATE FUNCTION [dbo].[GetAliasesById]
(
@userID int
)
RETURNS varchar(max)
AS
BEGIN
declare @output varchar(max)
select @output = COALESCE(@output + ', ', '') + alias
from UserAliases
where userid = @userID
return @output
END
GO
Happy coding.