0

I want to pass sys_refcursor as an argument to a procedure in PL/SQL. I have used the following codes to create a procedure

create or replace procedure reffunmani(
  cname varchar2,
  mysys out sys_refcursor) 
is
begin   
  open mysys for
    select /*c.ins_id,c.cname, c.start_date,*/i.ins_id,i.ins_name
      from course c,institution i where c.ins_id=i.ins_id 
     order by c.start_date;
end;
/
show errors;

and i have called the same procedure i an anonymous block

declare
  mysys sys_refcursor;
  rec institution%rowtype; 
begin
  reffunmani('MCA',mysys);
  loop
    fetch mysys into rec;
    exit when mysys%notfound;
    dbms_output.put_line(rec.ins_id||'    '||rec.ins_name);
  end loop;
  close mysys;
end;
/

When I execute my anonymous block, I get an error

ORA-06504: PL/SQL: Return types of Result Set variables or query do not match 
ORA-06512: at line 7

Note that the institution table has 5 columns.

4

1 回答 1

1

您正在将游标中的数据提取到局部变量rec中。该记录被定义为类型institution%rowtype。当且仅当游标实际返回表中的所有列(与institution表中定义的顺序相同)时,这才有效。由于这里不是这种情况,因此您有几个选择。

首先,您可以简单地定义一些标量变量并将数据提取到那些

declare
  mysys sys_refcursor;
  l_ins_id   institution.ins_id%type;
  l_ins_name institution.ins_name%type;
begin
  reffunmani('MCA',mysys);
  loop
    fetch mysys into l_ins_id, l_ins_name;
    exit when mysys%notfound;
    dbms_output.put_line(l_ins_id||'    '||l_ins_name);
  end loop;
  close mysys;
end;
/

或者,您可以声明具有两个字段的本地记录类型并将数据提取到该字段中

declare
  mysys sys_refcursor;

  -- I'm guessing at your data types here
  type typ_my_rec is record (
    ins_id    integer,
    ins_name  varchar2(100)
  );
  rec typ_my_rec; 
begin
  reffunmani('MCA',mysys);
  loop
    fetch mysys into rec;
    exit when mysys%notfound;
    dbms_output.put_line(rec.ins_id||'    '||rec.ins_name);
  end loop;
  close mysys;
end;
/
于 2015-07-01T14:20:54.393 回答