how to call one stored proc from anther and modify refcursor that is returned?

早过忘川 提交于 2019-12-18 05:25:08

问题


I have two stored procs, p_proc1 and p_proc2. p_proc1 returns a refcursor and I want to use the data in p_proc2. Is it possible call p_proc1 in p_proc2 and modify the dataset (outer join another table)? The database is Oracle.


回答1:


Not directly, no.

A SYS_REFCURSOR is a pointer to a result-- the only thing you can do with that is to fetch the data. You can't modify the result set.

P_PROC2 could fetch the data from the SYS_REFCURSOR, issue queries to get additional data from some other table, and return something to the caller. At that point, I would tend to favor turning P_PROC2 into a pipelined table function. But you could just return a collection with the modified data in it.

If p_proc2 absolutely needs to return a REF CURSOR, you could fetch the data from p_proc1's REF CURSOR into a global temporary table and then open a new cursor in p_proc2 that queries this global temporary table and does whatever additional manipulation you wish. Something like

SQL> create global temporary table tmp_emp
  2  as
  3  select empno, ename, deptno from emp where 1=2;

Table created.


SQL> create or replace procedure p1( p_cur1 out sys_refcursor )
  2  as
  3  begin
  4    open p_cur1 for select * from emp;
  5  end;
  6  /

Procedure created.

SQL> ed
Wrote file afiedt.buf

  1  create or replace procedure p2( p_cur2 out sys_refcursor )
  2  as
  3    l_cur1 sys_refcursor;
  4    l_rec  emp%rowtype;
  5  begin
  6    p1( l_cur1 );
  7    loop
  8      fetch l_cur1 into l_rec;
  9      exit when l_cur1%notfound;
 10      insert into tmp_emp( empno, ename, deptno ) values( l_rec.empno, l_rec
ename, l_rec.deptno );
 11    end loop;
 12    open p_cur2 for
 13      select empno, ename, dname
 14        from tmp_emp,
 15             dept
 16       where dept.deptno = tmp_emp.deptno;
 17* end;
SQL> /

Procedure created.

SQL> variable rc refcursor;
SQL> exec p2( :rc );

PL/SQL procedure successfully completed.

SQL> print rc

     EMPNO ENAME      DNAME
---------- ---------- --------------
      7839 KING       ACCOUNTING
      7934 MILLER     ACCOUNTING
      7782 CLARK      ACCOUNTING
      7369 smith      RESEARCH
      7902 FORD       RESEARCH
      7876 ADAMS      RESEARCH
      7788 SCOTT      RESEARCH
      7566 JONES      RESEARCH
      7900 JAMES      SALES
      7499 ALLEN      SALES
      7698 BLAKE      SALES

     EMPNO ENAME      DNAME
---------- ---------- --------------
      7654 MARTIN     SALES
      7844 TURNER     SALES
      7521 WARD       SALES

14 rows selected.


来源:https://stackoverflow.com/questions/3910022/how-to-call-one-stored-proc-from-anther-and-modify-refcursor-that-is-returned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!