母线低压出线 编号:sql结果查询

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/28 06:46:07
a b c

1 a 12
2 a 22
3 a 87
4 b 30
5 c 15
想要按照b栏位而把c栏位的信息组成字符串 如 b 栏位信息为a的 为 122287 请为这语句怎么写的阿!

create table t1(a int,b varchar(10))
insert t1 select 1,\'12\'
union all select 1,\'22\'
union all select 1,\'87\'
union all select 2,\'30\'
union all select 3,\'15\'

go

create function f(@a int)
returns varchar(8000)
as
begin
declare @re varchar(8000)
set @re=\'\'
select @re=@re+b from t1 where a=@a
return @re
end
go

select a,c=dbo.f(a)
from t1
group by a
go

drop table t1
drop function f