declare
type sp_emp_cursor is ref cursor;
test_cursor sp_emp_cursor;
v_ename pme.ename%type;
v_sal pme.sal%type;
begin
  open test_cursor for select ename,sal from pme where deptno=&deptno;
  loop
    fetch test_cursor into v_ename,v_sal;
    exit when test_cursor%notfound;(1)
    dbms_output.put_line('名字:'||v_ename||',工资'||v_sal);
  end loop;
end;
请问,如果没有(1)这句话,就是死循环,那么死循环为什么会老是打印最后一组数据呢?而不是打印空值什么的?

解决方案 »

  1.   

    我的理解是此时游标已经结束,不能再次进行FETCH,而你的变量v_ename,v_sal在最后一次是被赋值,变量并没有被释放,使用一直是打印变量的值,也就是最后一组数据
      

  2.   

    declare
    type sp_emp_cursor is ref cursor;
    test_cursor sp_emp_cursor;
    v_ename pme.ename%type;
    v_sal pme.sal%type;
    begin
      open test_cursor for select ename,sal from pme where deptno=&deptno;
      loop
        fetch test_cursor into v_ename,v_sal;
        exit when test_cursor%notfound;(1)
        dbms_output.put_line('名字:'||v_ename||',工资'||v_sal);
      end loop;
    close test_cursor;
    end;
    SQL> declare
      2  type sp_emp_cursor is ref cursor;
      3  test_cursor sp_emp_cursor;
      4  v_ename emp.ename%type;
      5  v_sal emp.sal%type;
      6  begin
      7    open test_cursor for select ename,sal from emp where deptno=&deptno;
      8    loop
      9      fetch test_cursor into v_ename,v_sal;
     10      exit when test_cursor%notfound;
     11      dbms_output.put_line('名字:'||v_ename||',工资'||v_sal);
     12    end loop;
     13  close test_cursor;
     14  end;
     15  /
    输入 deptno 的值:  10
    原值    7:   open test_cursor for select ename,sal from emp where deptno=&deptno;
    新值    7:   open test_cursor for select ename,sal from emp where deptno=10;
    名字:CLARK,工资2450
    名字:KING,工资5000
    名字:MILLER,工资1300PL/SQL 过程已成功完成。
      

  3.   

    是不是你数据库的问题啊,我的执行结果是最后输出组后一行很多,可是到这就结束了啊。提示ORA-20000: ORU-10027: buffer overflow, limit of 2000 bytes