--综合案例之瀑布模型
--统计每年入职员工人数
/*select to_char(hiredate,'yyyy')from emp;
-->光标-->循环-->退出条件:notfound
变量:1:初始值 2:如何得到
每年入职员工人数:
count80 number:=0;
count81 number:=0;
count82 number:=0;
count87 number:=0;
*/
declare
cursor cemp is select to_char(hiredate,'yyyy') from emp;
phiredate varchar2(4);
--每年入职员工人数
count80 number:=0;
count81 number:=0;
count82 number:=0;
count87 number:=0;
begin
--打开光标
open cemp;
--循环
loop
fetch cemp into phiredate;
exit when cemp%notfound;
--判断年份
if phiredate='1980' then count80:=count80+1;
elsif phiredate='1981' then count81:=count81+1;
elsif phiredate='1982' then count82:=count82+1;
else count87:=count87+1;
end if;
end loop;
close cemp;
--输出
DBMS_OUTPUT.PUT_LINE('1980:'||count80);
DBMS_OUTPUT.PUT_LINE('1981:'||count81);
DBMS_OUTPUT.PUT_LINE('1982:'||count82);
DBMS_OUTPUT.PUT_LINE('1987:'||count87);
end;
--
set SERVEROUTPUT ON;
--案例2:从最低工资涨起每人张10%,但工资总额不能超过5万
-------计算涨工资的人数和涨工资后的工作总额
/*selectempno,sal,from emp order bysal
-->光标-->循环-->退出条件:1.工资总额>5w 2.%notfound
2.变量
涨工资的人数:
countemp number:=0;
涨后工作总额:
saltotal number:
1 select sum(sal) into saltal from emp;
2.涨后工资总额=涨钱工资总额+sal*0.1
*/
declare
cursor cemp is select empno,sal from emp order by sal;
pempno EMP.ENAME%type;
psal EMP.SAL%type;
countemp number:=0;
saltal number;
begin
--得到工资初始值
select sum(sal) into saltal from emp;
open cemp;
loop
exit when saltal>500000;
--取每一个员工涨工资
fetch cemp into pempno,psal;
--
exit when cemp%notfound;
--涨工资
update emp set sal=sal*1.1 where empno=pempno;
--人数+1
countemp:=countemp+1;
--涨后工资
saltal:=saltal+sal*0.1;
end loop;
close cemp;
commit;
dbms_output.put_line('人数:'||countemp||'涨后工资'||saltal);
end;
--
oracle涨工资问题
最新推荐文章于 2022-06-13 16:30:48 发布