1

私は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 );

基本的に、テーブル固有のタスクを派生クラスに委譲する、テーブルに依存しないアクションを実行する方法を知っている単一の汎用ルーチンが必要です。

「参照カーソル」を使用する上記のオブジェクト メソッドはコンパイルされません - コンパイラは「カーソルを定義する必要がある」と言います。したがって、もちろん、「type generic_cursor as ref cursor」をあちこちで試しましたが、コンパイルできません。

参照カーソルをオブジェクト メソッドに渡すための構文を追跡しようとしても、ほとんど何も見つかりませんでした。そして、これは私が何かばかげたことをしようとしているのかもしれないと思いました。

私がやろうとしていることは理にかなっていますか?もしそうなら、私は何が欠けていますか? オブジェクト メソッドのパラメーターの型として使用できるように、generic_cursor をどこで定義できますか?

4

1 に答える 1

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 のものは通常の PL/SQL 情報とは別の本にあることを知っておく必要があると思います。困ったときはいつでも両方をチェックする必要があるでしょう。

于 2009-08-11T22:12:53.003 に答える