ORACLE pl/sql自定义函数、存储过程

本文介绍了如何在Oracle数据库中编写和使用PL/SQL自定义函数和存储过程,包括检查奖金、获取工资信息、查找经理详情、显示表数据、处理异常以及创建触发器等操作。通过实例详细解释了各种编程任务,如游标、程序包和触发器的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值