ORACLE pl/sql自定义函数、存储过程
2019-03-26
实验六、七 PL/SQL编程(2)
目的和要求:
1. 掌握存储过程和函数的使用
2. 掌握游标的使用
3. 掌握程序包的使用
4. 掌握触发器的使用
实验内容:
SCOTT用户拥有DEPT、EMP、SALGRADE表对象,其中,
DEPT是部门信息表,包括:部门编号、部门名称、部门地址三个属性,即DEPT(DEPTNO,DNAME,LOC)。
EMP是员工信息表,包括:员工编号、员工姓名、职位、上级领导、雇用时间、薪金、佣金、所属部门编号八个属性,即EMP(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)。
SALGRADE是工资等级表,包括:工资等级、最低工资、最高工资三个属性,即SALGRADE(GRADE,LOSAL,HISAL)。
set serveroutput on
describe emp;
1. 使用异常编写一个PL/SQL程序来检查职工号为7369的职工的奖金,如果没有奖金的话则显示出错信息。
参考答案(仅为参考,有些并不可以直接执行):
declare
comm_error exception;
v_comm emp.comm%type;
begin
select comm into v_comm from emp where empno=‘7369’;
if v_comm=0 or v_comm is null then
raise comm_error;
end if;
exception
when NO_DATA_FOUND then
dbms_output.put_line(‘无7369职工!’);
when comm_error then
dbms_output.put_line(‘无佣金!’);
end;
/体会预定义异常、非预定义异常、用户自定义异常的使用。
declare
comm_error exception;
v_comm emp.comm%type;
begin
select comm into v_comm from emp where empno=7369;
if v_comm=0 or v_comm is null then
raise comm_error;
end if;
exception
when NO_DATA_FOUND then
dbms_output.put_line('查无此人');
when comm_error then
dbms_output.put_line('无奖金');
end;
2. 编写一个函数来找出职工工资数额的信息,该函数还可以接受职工的号码。
create or replace function return_sal (eno scott.emp.empno%type)
return number
is
v_sal scott.emp.sal%type;
begin
select sal into v_sal from scott.emp where empno=eno;
return v_sal;
exception
when NO_DATA_FOUND then
dbms_output.put_line(‘无此职工!’);
end;
/
调用
declare
v_sal number(7,2);
begin
v_sal :=return_sal(7369);
dbms_output.put_line(v_sal);
end;
/
create or replace function return_sal (eno emp.empno%type)
return number
is
v_sal emp.sal%type;
begin
select sal into v_sal from emp where empno=eno;
return v_sal;
exception
when NO_DATA_FOUND then
dbms_output.put_line('无此职工!');
end;
/
调用
declare
v_sal number(7,2);
begin
v_sal :=return_sal(7369);
dbms_output.put_line(v_sal);
end;
/
3. 编写一个函数来找出职工的经理信息,该函数还可以接受职工的姓名。
create or replace function return_mgr (nowname emp.ename%type)
return number
is
v_mgr emp.mgr%type;
begin
select mgr into v_mgr from emp where ename=nowname;
return v_mgr;
exception
when NO_DATA_FOUND then
dbms_output.put_line('无此职工!');
end;
/
调用
declare
v_mgr number(4);
begin
v_mgr :=return_mgr(