oracle游标应用 sys_refcursor 和 cursor比较

来源:互联网 发布:python netsnmp 实例 编辑:程序博客网 时间:2024/05/16 06:53
 

http://blog.sina.com.cn/s/blog_7540bf5f0100qalz.html

sys_refcursor 和 cursor 优缺点比较

优点比较

优点一:sys_refcursor,可以在存储过程中作为参数返回一个table格式的结构集(我把他认为是table类型,容易理解,其实是一个游标集), cursor 只能用在存储过程,函数,包等的实现体中,不能做参数使用。

优点二:sys_refcursor 这东西可以使用在包中做参数,进行数据库面向对象开放。哈哈。我喜欢。cursor 就不能。       

缺点比较:

  缺点:sys_refcursor 不能用open,close ,fetch 进行操作。不好学,难理解。

            cursor  可以用 open,close ,fetch  操作,容易学,易懂

其他就目前不知道,至于游标的的基础概念,去google,百度一大堆的。这里就不累赘了。看例子:

建立一个存储过程

create or replace procedure up_test(o out sys_refcursor) is
begin
   open o for select * from lq_test;
end;

返回的类型是sys_refcursor;

建立第二个存储过程

 create or replace procedure up_getData(aMsg out varchar2) is
  type p_table_type is table of lq_test%rowtype;
  p_table p_table_type;
  v sys_refcursor;
begin
     up_test(v);
     fetch v bulk collect into p_table;
     for i in 1..p_table.count loop
        dbms_output.put_line('字段1:'||p_table(i).v1 || '   字段2:' || p_table(i).v2);
     end loop;
end;

这里要注意fetch 带参数的用法,bulk collect ,这是第集合的操作,必须先定义一个结合类。见上面的例子,还不懂就google了。用法就简单,没啥好介绍的。

取集合的值应该这样p_table(i).v1,其中i标识几行,带上字段,即可了。呵呵,容易理解。

原创粉丝点击