我使用 oracle 演示模式scott进行一些 plsql 测试(该模式中的数据永远不会改变)。我编写了以下程序来获取每个部门的员工编号。问题是,只有 4 个部门,但我的程序输出 5 行。我找不到原因,有人可以帮忙吗?十分感谢。
declare
cursor employees(department_id number) is
select count(*) howmany
from scott.emp
where deptno=department_id;
employees_per_dept employees%rowtype;
cursor departments is
select *
from scott.dept;
a_department departments%rowtype;
begin
dbms_output.put_line('-----------------------------------');
open departments;
loop
exit when departments%notfound;
fetch departments into a_department;
open employees(a_department.deptno);
fetch employees into employees_per_dept;
dbms_output.put_line(employees_per_dept.howmany);
close employees;
end loop;
close departments;
dbms_output.put_line('-----------------------------------');
end;