create function f_hb(@a int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str +','+ name from tablename where id= @a
set @str = right(@str , len(@str) - 1)
return(@str)
End
go
select distinct id ,dbo.f_hb(id) as b from tablename
create function fn_str(@id int)
returns varchar(200)
as
begin
declare @str varchar(200)
set @str=''
select @str=@str+','+[name] from T where id=@id
set @str=stuff(@str,1,1,'')
return @str
end
Go
select id,dbo.fn_str(id) as [name]
from T
group by id