在编写这个包的过程当中我出现的下面错误以后要记得更正:
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;