create function fn_test(@sid int)
returns varchar(100)
AS
begin
declare @str varchar(100)
set @str=''
select @str=@str+ACCOUNT+',' from 表A where sid=@sid
return @str
end
GO
insert into 表B
select SID ,fn_test(SID) as ACCOUNT
from 表A
group by SID
--测试数据
create table csdn(id int,txt varchar(10))
insert csdn
select 1,'a' union all
select 1,'b' union all
select 1,'c' union all
select 2,'aa' union all
select 2,'bb' union all
select 2,'cc' union all
select 3,'aaa' union all
select 3,'bbb'
--select * from csdn
go
create function Gettxt(@id int)
returns varchar(8000)
as
begin
declare @s varchar(8000)
set @s=''
select @s=@s +',' +txt from csdn where id=@id
--return @s
return stuff(@s,1,1,'')
end
go
select id,dbo.Gettxt(id) txt from csdn group by id
go