创建一个测试表:
-
create table test_weber(id number ,name varchar2(20) );
-
-
insert into test_weber values(1,\'weber\');
-
insert into test_weber values(2,\'roy\');
-
insert into test_weber values(3,\'ling\');
-
commit
然后继续创建存储过程:
-
create or replace procedure test_weber_proc(out_result_cursor out sys_refcursor) is
-
begin
-
open out_result_cursor for
-
select id,name from test_weber;
-
--close out_result_cursor
-
end
这里备注一下,以前习惯了open后随手关的习惯,结果导致在调用的时候报:ora 01001:invalid cursor的错误。
现在调用这个存储过程。
-
declare
-
e_result sys_refcursor;
-
c1 e_result%type;
-
arr test_weber%rowtype;
-
begin
-
test_weber_proc(c1);
-
-
loop
-
fetch c1
-
into arr;
-
dbms_output.put_line(arr.id || \'--\' || arr.name);
-
exit when c1%notfound;
-
end loop;
-
close c1;
-
end
这里强调一下,不能用for,必须老老实实的用open fetch into loop end loop. 原因是在刚才的procedure里就一直打开着cursor,而for循环会自动开启和关闭游标,cursor之前就开启了,现在又来开启,其实还有更深一层的关系这里就不给大家说了。主要是设计到for中定义的变量在引用内存的游标找不到类型。有想法的可以和我直接交流哦。邮箱是:
yaoweber@126.com
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29660117/viewspace-1378679/
,如需转载,请注明出处,否则将追究法律责任。