I tried the following:
declare @table table (id int, [name] varchar(10), gender char(1))
declare @MaleNames varchar(255)
insert into @table values (1,'John','M')
insert into @table values (2,'Sue','F')
insert into @table values (3,'Jane','F')
insert into @table values (4,'Mark','M')
insert into @table values (5,'Bill','M')
insert into @table values (6, null,'M')
insert into @table values (7,'Dingbat','M')
select @MaleNames = IsNull(@MaleNames + ', ','') + [name] from @table where gender = 'M' ORDER BY name
Select @MaleNames as MalePatients
Output is:
Bill, Dingbat, John, Mark