实际项目当中经常需要在一个存储过程中调用另一个存储过程返回的游标,本文列举了两种情况讲述具体的操作方法。
第一种情况:返回的游标是某个具体的表或视图的数据
create or replace procedure p_testa(presult out sys_refcursor) as
begin
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) as
begin
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;
本文深入探讨了SQL存储过程中如何调用另一个存储过程返回的游标,详细解析了两种常见情况:一是返回特定表或视图的所有数据;二是返回表中的特定列。通过实例代码演示了如何在存储过程中正确地使用游标进行数据检索。
3454

被折叠的 条评论
为什么被折叠?



