create function f(@nameid int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+cast(id as varchar) from B where nameid=@nameid
set @str=right(@str,(len(@str)-1))
return(@str)
End
go
--调用自定义函数得到结果
select distinct dbo.f(nameid)as 'id',nameid,namesstudent from B