create function dbo.f_str(@id int,@col1 varchar(10)) returns varchar(100)
as
begin
    declare @str varchar(1000)
    set @str = ''
    select @str = @str + ',' + cast(col2 as varchar) from tb where id = @id and col1 = @col1
    set @str = right(@str , len(@str) - 1)
    return @str
end
go--调用函数
select id , col1 , col2 = dbo.f_str(id,col1) from tb group by id,col1drop function dbo.f_str-----------------------------------
放在存储过程里面提示必须声明变量@id