ITPub博客

首页 > 数据库 > Oracle > oracle使用存储过程返回游标集(整张表)

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

原创 Oracle 作者:yaoweber 时间:2014-12-24 15:58:24 0 删除 编辑

创建一个测试表:

点击(此处)折叠或打开

  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博客 ” ,链接:https://blog.itpub.net/29660117/viewspace-1378679/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-05-15

  • 博文量
    18
  • 访问量
    62398