如何将ref cursor参数声明为对象方法?

时间:2009-08-11 21:17:25

标签: plsql ref-cursor

我对PL / SQL有点新,需要看起来像这样的东西:

create type base as object (
  unused number,
  member procedure p( c in ref cursor )
) not final;

create type child1 under base (
  overriding member procedure p( c in ref cursor ) as
    t table1%rowtype
  begin
    fetch c into t;
    -- process table1 row
  end;
);

create type child2 under base (
  overriding member procedure p( c in ref cursor ) as
    t table2%rowtype
  begin
    fetch c into t;
    -- process table2 row
  end;
);

procedure generic_handler( o in base, c in ref cursor ) as
begin
  o.p( c );
end;

o1 child1 := child1(0)
o2 child2 := child2(0)

c ref cursor
open c for select * from table1;
generic_handler( o1, c );

open c for select * from table2;
generic_handler( o2, c );

基本上,我需要一个通用例程,它知道如何执行一个独立于表的操作,将特定于表的任务委托给派生类。

以上对象方法采用'引用游标不编译 - 编译器说'游标需要定义'。所以当然我已经尝试'将generic_cursor作为引用游标'遍布整个地方,但无法将其编译。

在尝试跟踪将ref游标传递给对象方法的语法时,我发现了几乎 nothing 。这让我想到也许我正在尝试做一些愚蠢的事情。

我想做的事情有意义吗?如果是这样,我错过了什么?我在哪里可以定义generic_cursor,以便我可以将它用作对象方法参数类型?

1 个答案:

答案 0 :(得分:4)

一旦你解决了语法错误,你的代码就会起作用。

SQL> create or replace type base as object
  2  (  unused number
  3      ,  member procedure p( c in sys_refcursor )
  4  )
  5  not final;
  6  /

Type created.

SQL>
SQL> create or replace type child1 under base (
  2      overriding member procedure p( c in sys_refcursor )
  3  );
  4  /

Type created.

SQL> create or replace type body child1 as
  2      overriding member procedure p( c in sys_refcursor )
  3          as
  4              t dept%rowtype;
  5          begin
  6              loop
  7                  fetch c into t;
  8                  exit when c%notfound;
  9                  dbms_output.put_line('dname='||t.dname);
 10              end loop;
 11          end;
 12  end;
 13  /

Type body created.

SQL>
SQL> create or replace type child2 under base (
  2      overriding member procedure p( c in sys_refcursor )
  3   );
  4  /

Type created.

SQL> create or replace type body child2 as
  2      overriding member procedure p( c in sys_refcursor )
  3          as
  4              t emp%rowtype;
  5          begin
  6              loop
  7                  fetch c into t;
  8                  exit when c%notfound;
  9                  dbms_output.put_line('ename='||t.ename);
 10              end loop;
 11          end;
 12  end;
 13  /

Type body created.

SQL>
SQL>
SQL> create or replace procedure generic_handler
  2          ( o in out base, c in sys_refcursor )
  3          as
  4  begin
  5      o.p( c );
  6  end;
  7  /

Procedure created.

SQL>
SQL> set serveroutput on size unlimited
SQL>
SQL> declare
  2      o1 child1 := child1(0);
  3      o2 child2 := child2(0);
  4      rc sys_refcursor;
  5  begin
  6      open rc for select * from dept where deptno = 10;
  7      o1.p(rc);
  8      open rc for select * from emp where deptno = 10;
  9      o2.p(rc);
 10  end;
 11  /
dname=ACCOUNTING
ename=BOEHMER
ename=SCHNEIDER
ename=KISHORE

PL/SQL procedure successfully completed.

SQL>

当您是新手时,很难理解Oracle文档。我认为在您的情况下,您需要知道Object_Oriented stuffregular PL/SQL information不同。无论什么时候你都难过,你可能需要检查两者。