暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 参考游标-类型。cursor_type vs sys_refcursor

askTom 2017-03-07
1041

问题描述

编写的存储过程很少向调用事件发送引用游标。
他们正在使用类型。cursor_type作为数据类型。

类型有什么区别吗?cursor_type & SYS_REFCURSOR?

专家解答

具有定义的返回类型的游标是强类型的。Sys_ref游标是弱类型的。

关键的区别是,您只能将与返回类型匹配的查询分配给强游标。这是必须匹配的列的数量和类型 (尽管不是列名)。您可以使用弱类型光标打开任何查询:

create or replace package pkg as
  cursor cur is
    select 1 n from dual;
    
  type tcur is ref cursor return pkg.cur%rowtype;
  
end pkg;
/

declare
  cur pkg.tcur;
begin
  open cur for 
    select 'a' s, 1 n from dual;
end;
/

ORA-06550: line 5, column 5:
PLS-00382: expression is of wrong type

declare
  cur pkg.tcur;
begin
  open cur for 
    select sysdate d from dual;
end;
/

ORA-06550: line 5, column 5:
PLS-00382: expression is of wrong type

declare
  cur pkg.tcur;
begin
  open cur for 
    select 9 d from dual;
end;
/

PL/SQL procedure successfully completed.

declare
  cur sys_refcursor;
begin
  open cur for 
    select 'a' s, 1 n from dual;
    
  close cur;

  open cur for 
    select 9 n from dual;
   
  close cur;
end;
/

PL/SQL procedure successfully completed.
复制


最好使用强游标。查询必须匹配游标类型。如果不这样做,您的代码将无法编译!

为返回具有不同数量或列类型的游标的情况保留弱游标。虽然根据我的经验,这样做的必要性是罕见的!

进一步阅读:

http://docs.oracle.com/database/122/LNPLS/static-sql.htm#LNPLS568
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论