-- 1
declare
    type c is ref cursor;
    cur c;
begin
  open cur for select ename from emp;
  for rec in cur loop
      dbms_output.put_line(rec.ename);
  end loop;
end;上面的代码为什么会报:
PLS-00221: 'CUR' 不是过程或尚未定义而下面的代码却可以执行:-- 2
declare
  cursor cs is select ename from emp;
begin
for rec in cs loop
    dbms_output.put_line(rec.ename);
end loop;
end;
下面的代码也是可以运行的-- 3
declare
  type c is ref cursor;
  cur c;
  v_ename emp.ename%type;
begin
  open cur for select ename from emp;
  fetch cur into v_ename;
  while cur%found loop
      dbms_output.put_line(v_ename);
      fetch cur into v_ename;
  end loop;
  close cur;
end;
想请教一下为什么第一个利用 for 从游标变量 cur 中取数据会报错? 谢谢!

解决方案 »

  1.   

    利用 for 循环去遍历游标会自动打开, 关闭游标, 为什么第一个不会报:游标已打开, 
    而是报一个:"'CUR' 不是过程或尚未定义"   这样的错误, 期待回答, 谢谢!
      

  2.   

    declare
        type c is ref cursor;
        cur c;
    begin
       for c in (select ename from emp)
      loop
          dbms_output.put_line(c.ename);
      end loop;
    end;
      

  3.   

    declare
        cursor c is select ename from emp;
    begin
      for rec in c loop
          dbms_output.put_line(rec.ename);
      end loop;
    end;
    /
      

  4.   

    不要写open cursor,for会在执行开始打开游标,并在循环结束后关闭游标
      

  5.   


    --出现那个 我觉得是数据库不识别那个的游标变量
    SQL> edi
    已写入 file afiedt.buf  1  declare
      2  type c is ref cursor;
      3  cur c;
      4  begin
      5  for cur in (select ename from employyes)
      6  loop
      7  dbms_output.put_line(cur.ename );
      8  end loop;
      9* end;
    SQL> /
    wkc
    wkc168PL/SQL 过程已成功完成。