oracle函数返回一个列表,利用函数返回oracle对象表的三种方法

  • Post author:
  • Post category:其他


因为要返回表对象,无论后续用什么方法返回,都先要声明全局type;并且,字段变量类型要为object,不能为record:

create or replace type t_test as object(

EMPNO NUMBER(4),

ENAME VARCHAR2(10),

JOB VARCHAR2(9),

SAL NUMBER(7,2)

);

create or replace type t_test_table as table of t_test;

至于返回表对象的方法,目前发现三种:

1、用数组

create or replace function f_test_array(v_deptno in number default null)

return t_test_table

is

v_test t_test_table := t_test_table();

cursor cur is select empno, ename, job, sal from emp where deptno = v_deptno;

begin

for c in cur loop

v_test.extend();

v_test(v_test.count) := t_test(c.empno, c.ename, c.job, c.sal);

end loop;

return v_test;

end;

2、用pipe

create or replace function f_test_pipe(v_deptno in number default null)