用户输入一个雇员编号,根据它所在的部门给调整工资,规则:
10部门上涨20%,20部门降薪10%,30上涨30%,要求最高不能超过5000,最低不能低于750,如果超过5000就停留在5000,低于750就停留在750。
代码如下:
第一种:
--用户输入一个雇员编号,根据它所在的部门给调整工资,规则:10部门上涨20%,20部门降薪10%,30上涨30%,要求最高不能超过5000,最低不能低于750,如果超过5000就停留在5000,低于750就停留在750。
set serveroutput on
declare
--定义变量
v_eno EMP_TEMP.EMPNO%type;
v_sal EMP_TEMP.sal%type;
v_firstSal EMP_TEMP.sal%type;
v_dept EMP_TEMP.deptno%type;
begin
--手动输入指定eno
v_eno :=&empno;
--获取最初工资
select sal into v_firstSal from emp_temp where empno=v_eno;
--获取部门编号
select deptno into v_dept
from emp_temp
where empno=v_eno;
--如果部门编号是10,对其进行更新操作
if v_dept =10 then
update emp_temp
set sal =sal*1.2
where deptno =10;
--如果部门编号是20,对其进行更新操作
elsif v_dept =20 then
update emp_temp
set sal =sal*0.9
where deptno =20;
--如果部门编号是30,对其进行更新操作
elsif v_dept =30 then
update emp_temp
set sal =sal*1.3
where deptno =30;
end if;
select deptno into v_dept from emp_temp where empno=v_eno;
select max(sal)into v_sal from emp_temp where deptno=v_dept;
loop
--如果超过5000就停留在5000;
if v_sal>5000 then
update emp_temp
set sal =5000
where sal =v_sal and deptno =v_dept;
end if;
select max(sal) into v_sal from emp_temp where deptno=v_dept;
exit when v_sal<=5000;
end loop;
select min(sal) into v_sal from emp_temp where deptno=v_dept;
loop
--低于750就停留在750;
if v_sal<750 then
update emp_temp
set sal=750
where sal =v_sal and deptno=v_dept;
end if;
select min(sal) into v_sal from emp_temp where deptno=v_dept;
exit when v_sal>=750;
end loop;
dbms_output.put_line('编号为'||v_eno||' 部门为'||v_dept||' 刚开始工资为'||v_firstSal||' 变化后的工资为'||v_sal);
--错误提示
exception
when no_data_found then
dbms_output.put_line('查无此人!');
end;
/
运行结果截图:
第二种:
--用户输入一个雇员编号,根据它所在的部门给调整工资,规则:10部门上涨20%,20部门降薪10%,30上涨30%,要求最高不能超过5000,最低不能低于750,如果超过5000就停留在5000,低于750就停留在750。
set serveroutput on
declare
--定义变量
v_eno EMP_TEMP.EMPNO%type;
v_sal EMP_TEMP.sal%type;
v_firstSal EMP_TEMP.sal%type;
v_dept EMP_TEMP.deptno%type;
begin
--手动输入指定eno
v_eno :=&empno;
--获取最初工资
select sal,sal into v_firstSal,v_sal from emp_temp where empno=v_eno;
--获取部门编号
select deptno into v_dept
from emp_temp
where empno=v_eno;
--如果部门编号是10,对其进行更新操作
if v_dept =10 then
v_sal:=v_sal*1.2;
if v_sal>5000 then
v_sal:=5000;
end if;
if v_sal<750 then
v_sal:=750;
end if;
--如果部门编号是20,对其进行更新操作
elsif v_dept =20 then
v_sal:=v_sal*0.9;
if v_sal>5000 then
v_sal:=5000;
end if;
if v_sal<750 then
v_sal:=750;
end if;
--如果部门编号是30,对其进行更新操作
elsif v_dept =30 then
v_sal:=v_sal*1.3;
if v_sal>5000 then
v_sal:=5000;
end if;
if v_sal<750 then
v_sal:=750;
end if;
end if;
dbms_output.put_line('编号为'||v_eno||' 部门为'||v_dept||' 刚开始工资为'||v_firstSal||' 变化后的工资为'||v_sal);
--错误提示
exception
when no_data_found then
dbms_output.put_line('查无此人!');
end;
/
运行结果截图:
当输入的编号不存在,则执行exception报错部分。