oracle 在一个存储过程中调用另一个返回游标的存储过程

来源:互联网 发布:debian centos 编辑:程序博客网 时间:2024/05/21 10:08

        实际项目当中经常需要在一个存储过程中调用另一个存储过程返回的游标,本文列举了两种情况讲述具体的操作方法。

第一种情况:返回的游标是某个具体的表或视图的数据

create or replace procedure p_testa(presult out sys_refcursor) asbegin  open presult for    select * from users;end p_testa;
其中 users 就是数据库中一个表(或视图)。在调用的时候只要声明一个该表的rowtype类型就可以了:
create or replace procedure p_testb as  temp_cur sys_refcursor;  r        users%rowtype;begin  p_testa(temp_cur);  loop    fetch temp_cur      into r;    exit when temp_cur%notfound;    dbms_output.put_line(r.name);  end loop;end p_testb;

第二种情况:我们返回的不是表的所有的列,或许只是其中一列或两列 

create or replace procedure p_testa(presult out sys_refcursor) asbegin  open presult for    select id, name from users;end p_testa;
这里我们只返回了 users 表的 id, name 这两个列,那么调用的时候也必须做相应的修改: 
create or replace procedure p_testb as  temp_cur sys_refcursor;  cursor cur_1 is    select id, name from users where rownum = 1;  r cur_1%rowtype;begin  p_testa(temp_cur);  loop    fetch temp_cur      into r;    exit when temp_cur%notfound;    dbms_output.put_line(r.id);  end loop;end p_testb;