从oracle 9i 开始,提供了一个叫做“管道化表函数”的概念,来解决这个问题
这种类型的函数,必须返回一个集合类型,且标明 pipelined
这个oracle函数不能返回具体变量,必须以一个空 return 返回
这个oracle函数中,通过 pipe row () 语句来送出要返回的表中的每一行
调用这个oracle函数的时候,通过 table() 关键字把管道流仿真为一个数据集
具体写法如下:
create type row_type2 as object(username1 varchar2(100),dw1 varchar2(100),ts number); //创建行类型,同要返回的表的类型一致。
create type table_type2 as table of row_type2;
create or replace function fun1(dateSJ varchar2) return table_type2
pipelined as v row_type2;
begin
for myrow in
(
with a as
(
select (select username from users where userid= t.fromid) as username1,to_char((select dwbmmc from ks_xqpm_dwbm where dwbmid=(select deptid from users where userid=t.fromid) )) as dw1,count(*) as ts from mobile t where to_char(sendtime,'yyyy-mm')=dateSJ group by fromid
)
,
b as
(
select '合计' as username1,'' as dw1,(select count(*) from mobile where to_char(sendtime,'yyyy-mm')=dateSJ) as ts from dual
)
select username1,dw1,ts from a union all select username1,dw1,ts from b
)
loop v := row_type2(myrow.username1, myrow.dw1,myrow.ts);
pipe row (v);
end loop;
return;
end;
调用方法: select * from table(fun1('2012-01'));
注: