oracle使用存储过程返回游标集(整张表)

创建一个测试表:

点击(此处)折叠或打开

  1. create table test_weber(id number ,name varchar2(20) );

  2. insert into test_weber values(1,\'weber\');
  3. insert into test_weber values(2,\'roy\');
  4. insert into test_weber values(3,\'ling\');
  5. commit
然后继续创建存储过程:

点击(此处)折叠或打开

  1. create or replace procedure test_weber_proc(out_result_cursor out sys_refcursor) is
  2. begin
  3.   open out_result_cursor for
  4.     select id,name from test_weber;
  5. --close out_result_cursor
  6. end
这里备注一下,以前习惯了open后随手关的习惯,结果导致在调用的时候报:ora 01001:invalid  cursor的错误。
现在调用这个存储过程。

点击(此处)折叠或打开

  1. declare
  2.   e_result sys_refcursor;
  3.   c1 e_result%type;
  4.   arr test_weber%rowtype;
  5. begin
  6.   test_weber_proc(c1);

  7.   loop
  8.     fetch c1
  9.       into arr;
  10.        dbms_output.put_line(arr.id || \'--\' || arr.name);
  11.     exit when c1%notfound;
  12.      end loop;
  13.   close c1;
  14. end
这里强调一下,不能用for,必须老老实实的用open  fetch   into  loop    end  loop. 原因是在刚才的procedure里就一直打开着cursor,而for循环会自动开启和关闭游标,cursor之前就开启了,现在又来开启,其实还有更深一层的关系这里就不给大家说了。主要是设计到for中定义的变量在引用内存的游标找不到类型。有想法的可以和我直接交流哦。邮箱是: yaoweber@126.com

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29660117/viewspace-1378679/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29660117/viewspace-1378679/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值