while salTotal <= 50000
loop
fetch c1 into pempno, psal;--取出一条记录
exit when c1%notfound;
update emp set sal = sal * 1.1 where empno = pempno; --执行加薪
--记录涨工资后的总额
salTotal := salTotal + psal*0.1;
--记录涨工资的人数
empCount := empCount + 1;
end loop;
close c1;
commit;
dbms_output.put_line('涨工资人数:' || empCount || ' 工资总额:' || salTotal);
end;
/
示例2
l用PL/SQL语言编写一程序,实现按部门分段(6000以上、(6000,3000)、3000元以下)统计各工资段的职工人数、以及各部门的工资总额(工资总额中不包括奖金),参考如下格式:部门 小于3000数 3000-6000 大于6000 工资总额
10 2 1 0 8750
20 3 2 0 10875
30 6 0 0 9400
?
l提示:可以创建一张新表用于保存数据?
createtable msg1
(deptno number,
emp_num1 number,
emp_num2 number,
emp_num3 number,
sum_salnumber);
/*
用PL/SQL语言编写一程序,实现按部门分段(6000以上、(6000,3000)、3000元以下)统计各工资段的职工人数、
以及各部门的工资总额(工资总额中不包括奖金)
先写出可能用到的查询语句
a = select distinct deptno from dept;
select sal from emp where deptno= a中的某个值;
关于结果的输出:
1. 直接输出在屏幕上 2. 输出到一张表中 create table salcount (deptno number, --部门号 sg1 int, --3000以下的人数 sg2 int, -- 3000~6000的人数 sg3 int -- 6000以上的人数 ); */ declare --定义两个游标保存结果 cursor c1 is select distinct deptno from dept; cursor c2(pdno number) is select sal from emp where deptno=pdno; --定义三个变量用于保存每个部门三个工资段的人数 count1 NUMBER; count2 number; count3 number; --记录c1游标中的部门号 pdeptno dept.deptno% TYPE; --记录c2游标中的薪水值 psal emp.sal% TYPE; begin open c1;--打开c1 获得所有部门号 loop fetch c1 into pdeptno;--取一个部门号 exit when c1%notfound; --计数器清零 count1 := 0; count2 := 0; count3 := 0; --得到该部门的所有员工 open c2(pdeptno); loop fetch c2 into psal; --得到该员工的工资 exit when c2%notfound; if psal <=3000 then count1 := count1 + 1; elsif psal > 3000 and psal <=6000 then count2 := count2 + 1; else count3 := count3 + 1; end if; end loop; close c2; --保存该部门的统计结果 insert into salcount values(pdeptno,count1,count2,count3); commit; end loop; close c1; end;
/