大家好,欢迎来到IT知识分享网。
特点:
定义包头:
———————————————————————————————-
create_package ::=
———————————————————————————————-
定义包体:
———————————————————————————————-
create_package_body ::=
———————————————————————————————-
只有当包头编辑成功后才能编辑包体,其中的函数名与过程名须和包头中的函数过程一样。
包应用的一个例子:
包的作用: 根据出生年月返回年龄function Getage,返回工资function Getsalary
–创建环境
Create Table T_PsnSalary –工资表
(
Fpsncode varchar(4) default ”, –个人代码
Fpsndesc varchar(20) default ”, –描述
FpsnBirth varchar(20) default ”, –生日
FpsnSalary number(8,2) –工资
);
–添加数据
Insert into T_PsnSalary(Fpsncode,Fpsndesc,FpsnBirth,FpsnSalary) Values(‘C001′,’张三’,’1986.01.10′,1100);
Insert into T_PsnSalary(Fpsncode,Fpsndesc,FpsnBirth,FpsnSalary) Values(‘C002′,’李四’,’1980.10.10′,3000);
Insert into T_PsnSalary(Fpsncode,Fpsndesc,FpsnBirth,FpsnSalary) Values(‘C003′,’王五’,’1996.12.10′,800);
commit;
–创建包头
create or replace package A_GetData is
function Getage(birthst varchar,birthend varchar) return integer;
function Getsalary(VFpsncode varchar) return number;
end A_Getdata;
–创建包体
create or replace package body A_GETDATA is
function Getage(birthst varchar,birthend varchar) return integer –得到年龄函数
is
V_birth integer;
ToDateEnd Date;
Toyear number(4);
Tomonth number(4);
Fromyear number(4);
Frommonth number(4);
begin
if (birthend=”) or (birthend is null) then
select sysdate into ToDateEnd from dual; –得到系统时间
end if;
Toyear := to_number(to_char(ToDateEnd,’YYYY’)); –得到最后年月
Tomonth := to_number(to_char(ToDateEnd,’MM’));
Fromyear := to_number(substr(birthst,1,4));–计算的年月
Frommonth := to_number(substr(birthst,6,2));
if Tomonth-Frommonth>0 then V_birth:=Toyear-fromyear;
else V_birth:=Toyear-fromyear-1;
end if;
return(V_birth);
end Getage;
function getSalary(VFpsncode varchar) return number–返回工资情况
is
V_psnSalary number(8,2);
begin
Select FpsnSalary into V_psnSalary from T_PsnSalary where Fpsncode=VFpsncode;
return(V_psnSalary);
end getSalary;
end A_GETDATA;
–测试
select a.*,A_getdata.Getage(Fpsnbirth,”)age from T_psnsalary a; –调用包得到年龄功能
select A_getdata.Getsalary(‘C001’) from dual; –代码得到工资
参考:
1、 包头(Package)与包体(Package body)的学习与应用
2、 http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/create_package_body.htm#LNPLS01372
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29439655/viewspace-1071317/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net//viewspace-/
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/135028.html