笔记竟然没实现插入代码功能。
--创建表
/*create table msg
(deptno number,
count1 number,
count2 number,
count3 number,
saltotal number);
*/
set serveroutput on;
declare
--定义光标1:查询所有部门号
cursor cemp1 is select deptno from dept group by deptno;
pdeptno dept.deptno%type;--部门号
--定义光标2(部门号):带参光标,查询相应部门的所有员工工资
cursor cemp2(deptno2 dept.deptno%type) is select sal from emp where deptno=deptno2;
psal number;
psaltotal number;--相应部门工资总额
count1 number; --记录工资3000以下的员工人数
count2 number; --记录工资3000以上,6000以下的员工人数
count3 number; --记录工资6000以上的员工人数
begin
--打开光标
open cemp1;
--外层循环,得到所有部门号
loop
--得到一条部门号数据
fetch cemp1 into pdeptno;
exit when cemp1%notfound;
--重制所有临时变量
psaltotal:=0;
count1 :=0;
count2 :=0;
count3 :=0;
--如果cemp2未打开,则打开cemp2
if cemp2%isopen=false then
open cemp2(pdeptno);
end if;
--内层循环,得到相应部门员工的工资
loop
--得到一条相应部门号的员工数据
fetch cemp2 into psal;
exit when cemp2%notfound;
--判断工资级别
if psal<=3000 then count1 := count1+1;
elsif (psal>3000 and psal <=6000) then count2 :=count2+1;
else count3:=count3+1;
end if;
--计算工资总额 (比操作数据库更好)
psaltotal := psaltotal + psal;
end loop ;
--关闭cemp2
close cemp2;
--向msg中插入一条数据
insert into msg values(pdeptno,count1,count2,count3,psaltotal);
dbms_output.put_line('测试,插入成功');
end loop ;
--关闭光标
close cemp1;
end;
/
select * from msg;先看看