分类: Oracle
2009-07-21 09:27:36
--基于表emp和dept
--构造Procedure change_salary
--参数:ename in varchar2
salary in number
v_job out varchar2
v_dname out varchar2
--先查找指定员工,如果查出多条记录,提示并异常退出;如果没有该名员工,提示并异常退出。
--如果非上述情况,先判断该名员工的职位,如果职位不是'MANAGER',且要修改的薪水大于8000,拒绝修改并提示:“普通员工不能赚这么多薪水”。
--否则修改该名员工的薪水,
--输出参数:该职员的职位,和所在部门的名字,并打印输出。
--构造上述过程,并写出在SQL Editor中的调试代码(给出调用方式)
存储过程源代码如下:
create or replace procedure change_salary(
v_ename in emp.ename%TYPE,
v_salary in emp.sal%TYPE,
v_job out emp.job%TYPE,
v_dname out dept.dname%TYPE
)
is
v_empno emp.empno%TYPE;
too_many_salary exception;
begin
select e.empno, e.job, d.dname into v_empno, v_job, v_dname
from emp e, dept d
where e.deptno = d.deptno
and upper(ename) = upper(v_ename);
if v_job <> 'MANAGER' and v_salary > 8000 then
raise too_many_salary;
end if;
update emp set sal = v_salary where empno = v_empno;
if sql%found then
dbms_output.put_line('员工姓名为' || v_ename || '的员工的工资已经修改为' || v_salary);
end if;
exception
when no_data_found then
dbms_output.put_line('您输入的人员姓名' || '对应信息不存在');
when too_many_rows then
dbms_output.put_line('人员姓名为'||v_ename ||'不止一个,无法更新!');
when too_many_salary then
dbms_output.put_line('你输入的工资过高,无法录入');
when others then
dbms_output.put_line(sqlcode || sqlerrm);
end;
测试代码如下:
declare
v_out_job emp.job%type;
v_out_dname dept.dname%type;
begin
change_salary('jones', 8500, v_out_job, v_out_dname);
end;