7oracle实验七指导书--参考答案 下载本文

内容发布更新时间 : 2024/5/7 17:27:45星期一 下面是文章的全部内容请认真阅读。

实验七 过程和函数

【参考答案】:

1、存储过程

(1)针对emp表,编写存储过程Proc_AddSalary, 为指定的职员增加工资,要求两个输入参数:p_empid 指定员工id,p_salary 是需要增加的工资数量,要求检验员工加薪后工资上限,使用异常处理机制处理,最高为5000元,如果超出,则需要给出提示,并将此员工工资设置为5000元。(提示:参考示例1,但不同的是,本题要求创建存储过程实现相应功能)

create or replace procedure Proc_AddSalary(p_empid in NUMBER,p_salary in NUMBER) as

o_sal NUMBER(5); n_sal NUMBER(5); sal_exp EXCEPTION; begin

select sal into o_sal from scott.myemp where empno=p_empid; n_sal:=o_sal+p_salary; if n_sal>5000 then

update scott.myemp set sal=5000 where empno=p_empid; raise sal_exp; else

update scott.myemp set sal=n_sal where empno=p_empid; end if; exception

when no_data_found then

dbms_output.put_line(to_char(p_empid)||'无此职工'); when sal_exp then

dbms_output.put_line(to_char(p_empid)||'加薪后工资最高为5000'); end Proc_AddSalary; /

(2)编写存储过程Proc_getDepMaxSal,根据用户输入的Depid,从用户表(Emp)中查询此部门下的最高工资,并将此工资数返回给调用者。(提示:参考示例2)

create or replace procedure Proc_getDepMaxSa(Depid in myemp.deptno%type) as

Max_sal emp.sal%type; begin

select max(sal) into Max_sal from myemp where deptno=Depid;

dbms_output.put_line(to_char(Depid)||'的最高工资为'||to_char(Max_sal)); end Proc_getDepMaxSa; /

2、存储函数

(1)编写函数Fun_getAvgSalary, 根据用户输入的Depid,从用户表(Emp)中查询此部门下的平均工资,并将此工资数返回给调用者。(提示:参考示例3)

create or replace function Fun_getAvgSalary(Depid in myemp.deptno%type) Return NUMBER as

avg_sal NUMBER; begin

select avg(sal) into avg_sal from myemp where deptno=Depid; return(avg_sal);

end Fun_getAvgSalary; /

赋所创建的myemp表:

create table myemp as

select * from emp;