oracle plsql开发:存储过程综合练习

--基于表emp和dept
--构造Procedure change_salary
--参数:ename in varchar2
salary in number
v_job out varchar2
v_dname out varchar2
--先查找指定员工,如果查出多条记录,提示并异常退出;如果没有该名员工,提示并异常退出。
--如果非上述情况,先判断该名员工的职位,如果职位不是'MANAGER',且要修改的薪水大于8000,拒绝修改并提示:“普通员工不能赚这么多薪水”。
--否则修改该名员工的薪水,
--输出参数:该职员的职位,和所在部门的名字,并打印输出。

--构造上述过程,并写出在SQL Editor中的调试代码(给出调用方式)


[b]我自己写的过程:[/b]
create or replace procedure change_salary(v_ename in emp.ename%type,
v_salary in emp.sal%type,
v_job out varchar2,
v_dname out varchar2)
is
v_ecount int default 0;
r_emp emp%rowtype;
too_many_salary exception;
begin
select count(1) into v_ecount from emp where emp.ename=v_ename;
if v_ecount=0 then
raise NO_DATA_FOUND;
elsif v_ecount>1 then
raise TOO_MANY_ROWS;
else
begin
select * into r_emp from emp where emp.ename=v_ename;
if r_emp.job!='MANAGER' and v_salary>=8000 then
raise too_many_salary;
else
begin
update emp set sal=v_salary where emp.ename=v_ename;
commit;
v_job:=r_emp.job;
select dname into v_dname from dept where deptno=r_emp.deptno;
dbms_output.put_line('职位:'||v_job||',部门:'||v_dname);
end;
end if;
end;
end if;

exception
when NO_DATA_FOUND then dbms_output.put_line('未查询到该员工,请检查是否输入错误!');
when TOO_MANY_ROWS then dbms_output.put_line('查询到'||v_ecount||'个员工,请检查是否输入错误!');
when too_many_salary then dbms_output.put_line('普通员工不能赚这么多薪水!');
when others then null;
end change_salary;

[b]转载其他人的过程:[/b]
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;
[b]测试方式:[/b]
declare
v_job emp.job%type;
v_dname dept.dname%type;
begin
change_salary('BLAKE',8500,v_job ,v_dname );
if v_job is not null then
dbms_output.put_line('职位:'||v_job||',部门:'||v_dname);
end if;
end;


ps:若是刚刚安装的oracle ,emp和dept表 在scott/tiger 用户中存在的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值