"BULK COLLECT" 在 Oracle 中遇到无限循环

"BULK COLLECT" in Oracle runs into an infinite loop

我在 Oracle 的 SCOTT 中创建了如下 Package/Body。

-- PACKAGE SPECIFICATION:

create or replace package emp_pkg Is

    TYPE tbl_emp IS TABLE OF EMP%ROWTYPE;

    PROCEDURE p_displayEmpName;
   
end emp_pkg;
-- PACKAGE BODY:

create or replace package body emp_pkg Is
   
PROCEDURE p_displayEmpName IS   
    CURSOR c_rec IS select * from emp where deptno = 30;
    v_tbl_emp tbl_emp;
    BEGIN
        open c_rec;
        loop
            fetch c_rec bulk collect into v_tbl_emp;
            for i in 1..v_tbl_emp.count loop
                dbms_output.put_line(v_tbl_emp(i).ename || ','||v_tbl_emp(i).hiredate);
            end loop;
        end loop;
    END p_displayEmpName;
end emp_pkg;   

我编译包和正文没有问题。

一旦我执行了这个过程,它就进入了一个无限循环:

-- CALL ing the procedure:
exec emp_pkg.p_displayempname;

如何找出我做错了什么?

EXIT 没有更多行时的循环和 CLOSE 你的光标(是的,它应该隐式关闭,但最好养成良好的习惯,并在你工作时始终关闭它使用 language/driver,其中游标未隐式关闭):

create or replace package body emp_pkg Is
   
PROCEDURE p_displayEmpName
IS   
  CURSOR c_rec IS select * from emp where deptno = 30;
  v_tbl_emp tbl_emp;
  BEGIN
    open c_rec;
    loop
      EXIT WHEN c_rec%NOTFOUND;
      fetch c_rec bulk collect into v_tbl_emp LIMIT 50;

      for i in 1..v_tbl_emp.count loop
        dbms_output.put_line(v_tbl_emp(i).ename || ',' || v_tbl_emp(i).hiredate);
      end loop;
    end loop;

    CLOSE c_rec;
  END p_displayEmpName;
end emp_pkg;
/

db<>fiddle here