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)
return t_test_table PIPELINED
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
pipe row(t_test(c.empno, c.ename, c.job, c.sal));
end loop;
return;
end;
这两种都需要用游标遍历,得到表对象,性能上估计第2种较高。
3、用collect(不需要游标,代码相对简单)
create or replace function f_test_collect(v_deptno in number default null)
return t_test_table
is
v_test t_test_table := t_test_table();
begin
select t_test(empno, ename, job, sal) bulk collect into v_test from emp where deptno = v_deptno;
return v_test;
end;