create table 表(部门 int,人员 varchar(20))
insert into 表 select 1,'张三'
insert into 表 select 1,'李四'
insert into 表 select 1,'王五'
insert into 表 select 2,'赵六'
insert into 表 select 2,'邓七'
insert into 表 select 2,'刘八'
go--创建用户定义函数
create function f_str(@department int)
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+','+人员 from 表 where 部门 = @department
set @ret = stuff(@ret,1,1,'')
return @ret
end
go
--执行
select 部门,人员=dbo.f_str(部门) from 表 group by 部门 order by 部门
go--输出结果
/*
部门 人员
---- --------------
1 张三,李四,王五
2 赵六,邓七,刘八
*/
--删除测试数据
drop function f_str
drop table 表
go
insert into 表 select 1,'张三'
insert into 表 select 1,'李四'
insert into 表 select 1,'王五'
insert into 表 select 2,'赵六'
insert into 表 select 2,'邓七'
insert into 表 select 2,'刘八'
go--创建用户定义函数
create function f_str(@department int)
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+','+人员 from 表 where 部门 = @department
set @ret = stuff(@ret,1,1,'')
return @ret
end
go
--执行
select 部门,人员=dbo.f_str(部门) from 表 group by 部门 order by 部门
go--输出结果
/*
部门 人员
---- --------------
1 张三,李四,王五
2 赵六,邓七,刘八
*/
--删除测试数据
drop function f_str
drop table 表
go
insert into t select 'p1','c_a'
insert into t select 'p1','c_b'
insert into t select 'p1','c_c'
insert into t select 'p2','c_a'
insert into t select 'p2','c_b'
insert into t select 'p2','c_c'
go--创建用户定义函数
alter function f_str(@department varchar(20))
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+','+emp from t where dept = @department
set @ret = stuff(@ret,1,1,'')
return @ret
end
go
--执行
select dept,emp=dbo.f_str(dept) from t group by dept order by dept
go
--删除测试数据
drop function f_str
drop table t
go
(prodect varchar(100), ategory varchar(100))
insert into tb select 'p1','c_a'
insert into tb select 'p2','c_b'
insert into tb select 'p3','c_c'
insert into tb select 'p2','c_d'
insert into tb select 'p1','c_e'go
create function f_hb(@a varchar(100))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + ategory from tb where prodect = @a
set @str = right(@str , len(@str) - 1)
return(@str)
End
goselect distinct prodect ,dbo.f_hb(prodect) as b from tbdrop table tb
drop function f_hb
结果:
-------------------------
prodect b
------------------
p1 c_a,c_e
p2 c_b,c_d
p3 c_c
(所影响的行数为 3 行)
insert into test select 'p1','c_a' union all select 'p2','c_b' union all select 'p3','c_c' union all
select 'p2','c_d' union all select 'p1','c_3'select * from test
/*测试数据
prodect category
-------------------- --------------------
p1 c_a
p2 c_b
p3 c_c
p2 c_d
p1 c_3(所影响的行数为 5 行)
*/create function fun_AllString(@prodect varchar(20))
returns varchar(8000)
as
begin
declare @temp varchar(8000)
set @temp=''
select @temp=@temp+','+category from test where prodect=@prodect
set @temp=stuff(@temp,1,1,'')
return @temp
end
select prodect,dbo.fun_AllString(prodect) from test group by prodect/*
prodect
-------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
p1 c_a,c_3
p2 c_b,c_d
p3 c_c(所影响的行数为 3 行)
*/drop function dbo.fun_AllString
drop table test