1. 编写一个管理雇员信息的包emp_mgmt。包中有成员如下:
|
程序结构 |
类型 |
说明 |
|
Emp_count |
公有变量 |
跟踪员工的总人数变化,插入和删除员工时要修改该变量的值 |
|
init |
公有过程 |
初始化包,初始化员工人数和当前个人所得税率,建议有一个输入参数p_tax,传入当前个人所得税率,暂定为工资的8%。 |
|
tax_emp |
公有函数 |
通过员工编号计算出员工应交个人所得税款 |
|
Hire_emp |
公有过程 |
通过员工编号插入员工 |
|
Fire_emp |
公有过程 |
通过员工编号删除员工 |
|
emp_tax_record |
记录 |
用于游标C_emp的RETURN(强类型游标)语句中 |
|
C_emp |
游标 |
用于游标FOR循环中,会被过程show_emp_tax所使用 |
|
show_emp_tax |
公有过程 |
按工资升序输出所有雇员的应交所得税清单 |
|
Exist_emp |
私有函数 |
判断某个编号的员工是否存在,该函数会被hire_emp和fire_emp等过程调用 |
|
sal_null |
异常名 |
工资为空值时的异常名 |
步骤1:创建包头:
--包头创建
create or replace package emp_mgmt is
Emp_count number(4); --员工总数
p_tax float; --税率
sal_null Exception;
type emp_tax_record is record(e_no EMP.empno%type);--税率记录
cursor C_emp return emp_tax_record;--游标
function tax_emp(e_no EMP.empno%type) return float; --计算该员工的税
procedure init(v_tax float:=0.08); --初始化
procedure hire_emp(e_no in EMP.empno%type,e_name in emp.ename%type,e_job in emp.job%type,e_sal in emp.sal%type);--雇佣员工
procedure fire_emp(e_no in emp.empno%type);--炒掉员工
procedure show_emp_tax;--员工工资高低
function Exist_emp(e_no in emp.empno%type) return boolean;--查找该员工是否存在
function getEname(e_no in emp.empno%type) return varchar2;--获得员工名字
procedure getOneEmpTax(e_no in emp.empno%type); --显示查询员工的税
function getEmpCount return number; --返回员工总数
end emp_mgmt;
create or replace package body emp_mgmt as
cursor C_emp return emp_tax_record is select empno from emp order by sal asc;
function getEmpCount return number is
xx number(4);
begin
select count(*) into xx from emp;
return xx;
end getEmpCount;
procedure show_emp_tax as
xx emp.empno%type;
begin
if NOT C_emp%ISOPEN then open C_emp;
end if;
loop
fetch C_emp into xx;
exit when C_emp%NOTFOUND;
dbms_output.put_line(xx||getEname(xx)||'的税为:'||to_char(tax_emp(xx)));
end loop;
close C_emp;
end show_emp_tax;
function getEname(e_no in emp.empno%type) return varchar2 as
xx varchar2(100);
begin
select ename into xx from emp where empno=e_no;
return xx;
end getEname;
procedure init(v_tax float:=0.08) as
begin
p_tax:=v_tax;
Emp_count:=getEmpCount();
dbms_output.put_line('当前税率为:'||p_tax*100||'%');
dbms_output.put_line('当前员工数为:'||Emp_count);
end init;
procedure hire_emp(e_no in EMP.empno%type,e_name in emp.ename%type,e_job in emp.job%type,e_sal in emp.sal%type) as
begin
if(Exist_emp(e_no)) then raise_application_error(-20000,e_no||'该编号的员工已经存在!');
end if;
insert into emp(empno,ename,job,sal) values(e_no,e_name,e_job,e_sal);
emp_count:=emp_count+1;
dbms_output.put_line('当前员工总数为:'||emp_count);
end hire_emp;
procedure fire_emp(e_no in EMP.empno%type) as
begin
if(not Exist_emp(e_no)) then raise_application_error(-20000,e_no||'该编号的员工不存在!');
end if;
delete from emp where empno=e_no;
emp_count:=emp_count-1;
dbms_output.put_line('当前员工总数为:'||emp_count);
end fire_emp;
function tax_emp(e_no in EMP.empno%type) return float is
v_tax number(7,2):=0;
begin
select sal into v_tax from emp where empno=e_no;if v_tax=0 then raise sal_null;
end if;
v_tax:=v_tax*p_tax;
return v_tax;
exception
when no_data_found then dbms_output.put_line('员工不存在!');
when sal_null then dbms_output.put_line('有工资为空!');
end tax_emp;
function Exist_emp (e_no in emp.empno%type) return boolean is
vv number(2):=0;
begin
select count(*) into vv from emp where empno=e_no;if vv=0 then return false;elsif vv>0 then return true;end if;
end Exist_emp;
procedure getOneEmpTax(e_no in emp.empno%type) as
begin
dbms_output.put_line(e_no||'的税收为'||tax_emp(e_no));
end getOneEmpTax;
end emp_mgmt;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/27042095/viewspace-739585/ ,如需转载,请注明出处,否则将追究法律责任。