如何从花药调用一个存储过程并修改返回的引用游标?

发布于 2024-09-27 04:34:25 字数 198 浏览 7 评论 0原文

我有两个存储过程,p_proc1p_proc2p_proc1 返回一个引用游标,我想使用 p_proc2 中的数据。是否可以在p_proc2中调用p_proc1并修改数据集(外部连接另一个表)?数据库是Oracle。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几味少女 2024-10-04 04:34:25

不直接,不。

SYS_REFCURSOR 是一个指向结果的指针——您唯一能做的就是获取数据。您无法修改结果集。

P_PROC2 可以从 SYS_REFCURSOR 获取数据,发出查询以从其他表获取附加数据,然后将某些内容返回给调用者。那时,我倾向于将 P_PROC2 转变为流水线表函数。但是您可以只返回一个包含修改后的数据的集合。

如果 p_proc2 绝对需要返回 REF CURSOR,您可以将 p_proc1 的 REF CURSOR 中的数据提取到全局临时表中,然后在 p_proc2 中打开一个新游标来查询此全局临时表并执行您希望的任何其他操作。像这样的东西

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.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文