oracle数据库包的使用

在编写这个包的过程当中我出现的下面错误以后要记得更正:

1.存储过程的out形式参数不能有默认值。

2.if else语句的最后我掉了end if;

create or replace package hdb_pck is
  --函数validate_deptno当中使用
  b_isdept boolean;
  --函数声明部分
  function validate_deptno(in_deptno emp.deptno%type) return boolean;
  --过程声明部分
  procedure get_sumsal_dept(in_deptno emp.deptno%type, p_sum out number);
  procedure get_avgsal_dept(in_deptno emp.deptno%type, p_avg out number);
end;

create or replace package body hdb_pck is
--首先定义函数,该函数的目的是为了检测部门是否在员工表中存在
function validate_deptno(in_deptno emp.deptno%type) return boolean is
v_count number := 0;
begin
  select count(*) into v_count from emp e where e.deptno = in_deptno;
  if v_count = 0 then b_isdept := false;
  elsif v_count > 0 then b_isdept := true;
  end if;
  return b_isdept;
exception
when others then
  raise_application_error(-20001, '函数validate_deptno()在运行时出错!');
end validate_deptno;
--定义过程get_sumsal_dept的实现体
procedure get_sumsal_dept(in_deptno emp.deptno%type, p_sum out number) is
begin
if validate_deptno(in_deptno) then
  select sum(e.sal) into p_sum from emp e where e.deptno = in_deptno;
end if;
exception
when others then
   raise_application_error(-20002, '过程get_sumsal_dept()在运行时出错!');
end get_sumsal_dept;
--定义过程get_avgsal_dept的实现体
procedure get_avgsal_dept(in_deptno emp.deptno%type, p_avg out number) is
begin
if validate_deptno(in_deptno) then
  select avg(e.sal) into p_avg from emp e where e.deptno = in_deptno;
end if;
exception
when others then
  raise_application_error(-20003, '过程get_avgsal_dept()在运行时出错!');
end get_avgsal_dept;

end;

--测试上面新建的包的程序块
declare var_deptno emp.deptno%type := &no; v_sum emp.sal%type; v_avg emp.sal%type;
begin
case hdb_pck.validate_deptno(var_deptno)
  when true then dbms_output.put_line('你所输入的部门号:' || var_deptno || '有员工');
    hdb_pck.get_sumsal_dept(var_deptno, v_sum);
    dbms_output.put_line('该部门的薪水总和是:' || v_sum);
    hdb_pck.get_avgsal_dept(var_deptno, v_avg);
    dbms_output.put_line('该部门的平均薪水是:' || v_avg);
  when false then
    dbms_output.put_line('你所输入的部门号不存在!');
end case;
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值