Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...