/*
SQL语句
select empno,sal from emp order by sal;
-->光标-->循环-->退出条件:1.工资总额大于5W,2%notfound
变量:1.初始值 2.如何得到
涨工资的人数:
countEmp number := 0;
涨后的工资总量:
salTotal number;
1.select sum(sal) into salTotal from emp;
2.涨后的工资总额=涨前的工资总额+sal*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;
--3.如果涨工资后总额超过5W
exit when salTotal + psal*0.1 > 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;
end;
/
set serveroutput on
declare
--定义光标
cursor emp_cursor is select empno,sal from emp order by sal;
--定义变量
v_empno emp.empno%type;
v_sal emp.sal%type;
--涨工资的人数
v_countemp number := 0;
--涨工资的总额
v_totalsal number;
begin
rollback;
--为涨工资总额赋值初始值
select sum(sal) into v_totalsal from emp;
--打开光标
open emp_cursor;
loop
fetch emp_cursor into v_empno,v_sal;
exit when emp_cursor%notfound;
if(v_totalsal + v_sal * 0.1) < 50000 then
-- 涨工资
update emp set sal = sal*1.1 where empno = v_empno;
--计算人数
v_countemp := v_countemp + 1;
--计算涨后工资总额
v_totalsal := v_totalsal + v_sal * 0.1;
else exit;
end if;
end loop;
--关闭光标
close emp_cursor;
commit;
--输出数据
dbms_output.put_line('涨工资的人数有:'||v_countemp||'人');
dbms_output.put_line('涨后工资总额:'||v_totalsal||'元');
end;
/
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
exit when salTotal > 50000;
fetch cemp into pempno,psal;
exit when cemp%notfound;
update emp set sal = sal*1.1 where empno =pempno;
countEmp := countEmp +1;
salTotal:=salTotal + psal*0.1;
end loop;
close cemp;
dbms.output.put_line();
end;
/
给员工涨工资,pl/sql
SET serveroutput ON; DECLARE CURSOR cemp IS SELECT sal,ename,empno FROM emp ORDER BY sal ASC; pename emp.ename%type; --员工名称 psal emp.sal%type; -- 员工工资 pempno emp.empno%type; --员工编号 person NUMBER:=0; --人数 allSal NUMBER; --总工资 sal_toMany EXCEPTION; --异常 BEGIN rollback; SELECT SUM(sal) INTO allSal FROM emp; --总金额; dbms_output.put_line('初始总金额为:'||allSal); OPEN cemp; LOOP IF allSal>55000 THEN --每次循环先判断工资总额有没有超过5万元。 raise sal_toMany; END IF; FETCH cemp INTO psal,pename,pempno; allSal :=allSal+ psal*0.1; --总工资额增加 psal := psal + psal*0.1; --增长百分之10% person :=person+1; --人数加1 dbms_output.put_line('涨工资的员工名称'||pename||'涨完金额为:'||psal); update emp set sal = psal where empno = pempno; EXIT WHEN cemp%notfound; END LOOP; dbms_output.put_line('涨工资后的工资总额:'||allSal); dbms_output.put_line('涨工资后的人数:'||person); CLOSE cemp; commit; EXCEPTION WHEN sal_toMany THEN dbms_output.put_line('金额已超过5.5万元'); dbms_output.put_line('涨工资后的工资总额:'||allSal); dbms_output.put_line('涨工资后的人数:'||person); END;