--生成测试数据
create table t(
id int,
f1 int,
f2 char(1))insert into t select 1,10,'A'
insert into t select 2,10,'B'
insert into t select 3,10,'C'
insert into t select 4,20,'D'
insert into t select 5,20,'E'
insert into t select 6,20,'F'
insert into t select 7,20,'G'--创建自定义函数
create function f_str(@f1 varchar(10))
returns varchar(8000)
as
begin
declare @f2 varchar(8000)
set @f2=''
select @f2=@f2+'、'+rtrim(f2)
from t where f1=@f1
return(stuff(@f2,1,1,''))
end
--执行查询
select f1,dbo.f_str(f1) from t group by f1
create table t(
id int,
f1 int,
f2 char(1))insert into t select 1,10,'A'
insert into t select 2,10,'B'
insert into t select 3,10,'C'
insert into t select 4,20,'D'
insert into t select 5,20,'E'
insert into t select 6,20,'F'
insert into t select 7,20,'G'--创建自定义函数
create function f_str(@f1 varchar(10))
returns varchar(8000)
as
begin
declare @f2 varchar(8000)
set @f2=''
select @f2=@f2+'、'+rtrim(f2)
from t where f1=@f1
return(stuff(@f2,1,1,''))
end
--执行查询
select f1,dbo.f_str(f1) from t group by f1
Create Function AddStr(@fl int)
returns varchar(1000)
as
begin
declare @Str varchar(3000)
select @Str=@Str+'、'+'f2' from tablename where @fl=fl
returns(stuff(@Str,1,1,''))
endselect distinct fl=dbo.AddStr(fl),f2 from tablename