我有一个有7列的表,上面有关于员工的信息,如下所示,但行数要多得多,并将随着新员工的到来而添加更多.
emp_numb | salary | dept_num | etc | etc | etc | etc |
---|---|---|---|---|---|---|
0001 | 111111 | 10 | Cell 2 | Cell 1 | Cell 2 | Cell 1 |
0002 | 222222 | 20 | Cell 2 | Cell 1 | Cell 2 | Cell 1 |
0003 | 333333 | 30 | Cell 2 | Cell 1 | Cell 2 | Cell 1 |
0004 | 444444 | 10 | Cell 2 | Cell 1 | Cell 2 | Cell 1 |
我正在try 编写一个PLSQL程序,这样我就可以看到每个部门工资最低的2名员工(如果稍后我添加了更多的部门编号,则需要能够使用此程序). 问题是,对于我到目前为止编写的代码,我陷入了无限循环,无法理解到目前为止我在哪里失败了.如果问题不是很多,因为我是SQL的新手,而且我的知识也不是很丰富,那么有人可以判断我的代码并告诉我哪里出了问题吗?提前感谢!!
我的代码如下:
create or replace procedure lowemployees
as
cursor cur1 is select * from emple order by dept_no,salario;
empleadus emple%rowtype;
i number;
depcursor emple.dept_no%type;
depcompare emple.dept_no%type;
begin
i:=0;
open cur1;
fetch cur1 into empleadus;
depcursor:=empleadus.dept_no;
depcompare:=depcursor+1;
while cur1%found loop
while i<2 loop
dbms_output.put_line(empleadus.emp_no||'|'||empleadus.nombre||'|'||empleadus.oficio||'|'||empleadus.salario||'|'||empleadus.dept_no);
i:=i+1;
fetch cur1 into empleadus;
end loop;
depcursor:=empleadus.dept_no;
depcompare:=depcursor+1;
while depcursor<depcompare loop
fetch cur1 into empleadus;
i:=0;
depcursor:=empleadus.dept_no;
end loop;
dbms_output.put_line(empleadus.emp_no||'|'||empleadus.nombre||'|'||empleadus.oficio||'|'||empleadus.salario||'|'||empleadus.dept_no);
depcompare:=depcursor+1;
fetch cur1 into empleadus;
end loop;
close cur1;
end lowemployees;