-------------参考例子
--建立测试环境
create table tb2 (id int,type nvarchar(10))
insert into tb2 select 1,'a'
insert into tb2 select 1,'b'
insert into tb2 select 1,'c'
insert into tb2 select 1,'d'
insert into tb2 select 2,'a'
insert into tb2 select 3,'b'
insert into tb2 select 4,'c'
insert into tb2 select 4,'d'
go
--查询处理
create function f_catString(@id int)
returns nvarchar(1000)
as
begin
declare @s nvarchar(1000)
set @s=''
select @s=@s+','+type from tb2 where id=@id
return(stuff(@s,1,1,''))
end
go
--调用函数
select id,dbo.f_catString(id) as type from tb2 group by id
go
--在数据库中建立一个处理的自定义函数
create function f_name(@id int)
returns varchar(8000)
as
begin
declare @ varchar(8000)
set @=''
select @=@+','+rtrim(name)
from 表
where id=@id
return(stuff(@,1,1,''))
end
go
--前台显示的时候,使用下面的语句来调用
select id,name=dbo.f_name(id) from 表 group by id