Oracle管道函数(Pipelined Table Function)
oracle管道函数
1、管道函数即是可以返回行集合(可以使嵌套表nested table 或数组 varray)的函数,我们可以像查询物理表一样查询它或者将其赋值给集合变量。
2、管道函数为并行执行,在普通的函数中使用dbms_output输出的信息,需要在服务器执行完整个函数后一次性的返回给客户端。如果需要在客户端
实时的输出函数执行过程中的一些信息,在oracle9i以后可以使用管道函数(pipeline function)。
3、关键字PIPELINED表明这是一个oracle管道函数,oracle管道函数的返回值类型必须为集合,在函数中,PIPE ROW语句被用来返回该集合的单个元素,函数以一个空的RETURN 语句结束,以表明它已经完成。
原文:https://blog.csdn.net/indexman/article/details/27580517
1.简单例子
-
--PIPELINED 表示管道函数 -
--PIPE ROW 返回该集合的单个元素 -
--创建type类型 t_table -
create or replace type t_table is table of number -
--创建函数 -
create or replace function f_pipe(s number) -
return t_table pipelined --返回t_table -
as -
v_number number; -
begin -
for i in 1..s loop -
v_number := i; -
pipe row(v_number);--返回集合单个的值 -
end loop; -
return; -
end f_pipe; -
--测试函数 -
select * from table(f_pipe(5))
结果图

2.复杂一点的例子
-
--创建一个表类型 -
create or replace type obj_table as object -
( -
id int, -
name varchar2(50) -
) -
--创建返回多个表类型的类型 -
create or replace type list_table is table of obj_table -
--创建函数 -
create or replace function f_pipes(s number) -
return list_table pipelined --list_table返回类型 -
as -
v_obj_table obj_table; -
begin -
for i in 1..s loop -
v_obj_table := obj_table(i,to_char(i*i)); -
pipe row(v_obj_table);--返回obj_table类型的单列数据 -
end loop; -
return; -
end f_pipes; -
--测试函数 -
select * from table(f_pipes(5))
结果图:




















