ORACLE SYS_REFCURSOR VS REF CURSOR

CURSOR是强类型,SYS_REFCURSOR 是弱类型(类似C#的var)。

Cursor:

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;
/

--正常运行
declare
  cur pkg.tcur;
begin
  open cur for 
    select 'kkk' n from dual;
end;
/

--正常运行
declare
  cur pkg.tcur;
begin
  open cur for 
    select 900 n from dual;
end;
/
View Code

 

Sys_refcursor:

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;
/

 

posted @ 2020-06-18 17:41  舒碧  阅读(558)  评论(0编辑  收藏  举报