【问题标题】:Unable to use Joins in Sys RefCursor in Oracle无法在 Oracle 的 Sys RefCursor 中使用联接
【发布时间】:2015-07-01 04:39:17
【问题描述】:

我想将 sys_refcursor 作为参数传递给 PL/SQL 中的过程。 我已经使用以下代码创建了一个过程

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;

我已经将相同的过程称为匿名块

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

当我执行我的匿名块时,我得到一个错误

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

请注意,institution 表有 5 列。

【问题讨论】:

  • mysys 是一个局部变量,因此您不会在它前面加上冒号(无论您使用什么前端工具,它都可能解释为绑定变量)。只需reffunmani('MCA',mysys);
  • 是的,我试过但没有用得到同样的错误信息
  • 去掉冒号时包含错误编号的完整错误堆栈是什么?你的institution 表真的只有两列吗?
  • 没有,我的机构表有 5 列,而课程表有 5 列..
  • ORA-06504:PL/SQL:结果集变量或查询的返回类型不匹配 ORA-06512:第 7 行

标签: oracle join sys-refcursor


【解决方案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;
/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多