时间:2021-07-01 10:21:17 帮助过:21人阅读
实例2:为员工涨工资
从最低工资调起每人涨10%,但工资总额不能超过5万元,请计算涨工资的人数和涨工资后的工资总额,并输出涨工资人数及工资总额。
分析:
SQL语句:select empno,sal from emp order by sal;
--->光标--->退出:1.总额>5w 2.notfound
变量:1.初始值 2.最终得到
涨工资的人数:countEmp number :=0;
涨后的工资总额:salTotal number;
1.select sum(sal) from salTotal from emp;
2.涨后=涨前*0.1
set serveroutput on declare cursor cemp is select empno,sal from emp order by sal; pempno emp.empno%type; psal emp.sal%type; --涨工资的人数: countEmp number :=0; -- 涨后的工资总额: salTotal number; begin --得到初始的工资总额 select sum(sal) into salTotal from emp; open cemp; loop --1.总额>5w exit when salTotal>50000; --取一个员工 fetch cemp into pempno,psal; --2.notfound exit when cemp%notfound --涨工资(涨工资之前进行判断一下,如果多于50000就不涨) update emp set sal =sal*1.1 where empno =pempno; --人数+1 countEmp :=countEmp +1; --2.涨后=涨前+sal*0.1(当没有必要进行操作数据库时就不进行操作) salTotal :=salTotal+psal*0.1 end loop; close cemp; commit; dbms_output.put_line(‘人数:‘||countEmp||‘总金额:‘salTotal) end; /
实例3:实现按部门分段(6000以上,(6000,3000),3000以下)统计各工资段的职工人数,以及各部门的工资总额
SQL语句:
部门:select deptno from dept;
部门中员工的薪水:select sal from emp where deptno =???
变量:1.初始值 2.最终得到
每个段的人数:
count1 number; count2 number ;count3 number
部门的工资总额: salTatal number :=0
1,select sum(sal) into salTotal from emp where deptno=???
2.将部门当中所有员工的薪水都加起来
set serveroutput on declare --部门(定义光标就要定义变量) cursor cdept is select deptno from dept; pdeptno dept.deptno%type; --部门中员工的薪水 cursor cemp(dno number) is select sal from emp where deptno =dno; psal emp.sal%type; --每个段的人数: count1 number; count2 number ;count3 number --部门的工资总额: salTatal number :=0 begin open cdept; loop --取一个部门(取完就应该想到退出条件) fetch cdept into pdeptno; exit when cdept%notfound; --初始化 count1:=0;count2:=0;count3:=0; --部门的工资总额 select sum(sal) into salTotal from emp where deptno = pdeptno --取部门中员工的薪水 open cemp(pdeptno); loop fetch cemp into psal; exit when cemp%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 cemp; --保存结果 insert into msg values(pdeptno,count1,count2,count3,nvl(saltotal,0)) end loop; close cdept; commit; dbms_output.put_line(‘完成‘) end; /
PLSQL语句编写步骤
标签:server 语句 loop put round div style salt varchar