oracle package是oracle包,是一組相關過程、函式、變數、常量#SinaEditor_Temp_FontName、類型和游標等PL/SQL程式設計元素的組合。包具有面向對象設計的特點,是對這些PL/SQL程式設計元素的封裝。
基本介紹
- 中文名:oracle package
- 公司:oracle 公司
- 定義:相關過程、函式、變數、常量
- 語言:英語
create or replace package<Package_name>istype<TypeName>is<Datatype>;--定義類型--Publicconstantdeclarations<ConstantName>constant<Datatype>:=<Value>;--聲明常量--Publicvariabledeclarations<VariableName><Datatype>;--數據類型--Publicfunctionandproceduredeclarationsfunction<FunctionName>(<Parameter><Datatype>)return<Datatype>;--函式end<Package_name>;定義包體:--create or replace packagebody<Package_name>is--Privatetypedeclarationstype<TypeName>is<Datatype>;--Privateconstantdeclarations<ConstantName>constant<Datatype>:=<Value>--Privatevariabledeclarations<VariableName><Datatype>;--Functionandprocedureimplementationsfunction<FunctionName>(<Parameter><Datatype>)return<Datatype>is--函式的具體內容<LocalVariable><Datatype>;begin<Statement>;return(<Result>);end;begin--Initialization--初始化包體,每次調用時被初始化<Statement>;end<Package_name>;
--創建環境CreateTableT_PsnSalary--工資表(Fpsncodevarchar(4)default'',--個人代碼Fpsndescvarchar(20)default'',--描述FpsnBirthvarchar(20)default'',--生日FpsnSalarynumber(8,2)--工資);--添加數據InsertintoT_PsnSalary(Fpsncode,Fpsndesc,FpsnBirth,FpsnSalary)Values('C001','張三','1986.01.10',1100);InsertintoT_PsnSalary(Fpsncode,Fpsndesc,FpsnBirth,FpsnSalary)Values('C002','李四','1980.10.10',3000);InsertintoT_PsnSalary(Fpsncode,Fpsndesc,FpsnBirth,FpsnSalary)Values('C003','王五','1996.12.10',800);commit;--創建包頭createorreplacepackagepackage_demoisfunctionGetage(birthstvarchar,birthendvarchar)returninteger;functionGetsalary(VFpsncodevarchar)returnnumber;endpackage_demo;--創建包體createorreplacepackagebodypackage_demoisfunctionGetage(birthstvarchar,birthendvarchar)returninteger--得到年齡函式isV_birthinteger;ToDateEndDate;Toyearnumber(4);Tomonthnumber(4);Fromyearnumber(4);Frommonthnumber(4);beginif(birthend='')or(birthendisnull)thenselectsysdateintoToDateEndfromdual;--得到系統時間endif;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));ifTomonth-Frommonth>0thenV_birth:=Toyear-fromyear;elseV_birth:=Toyear-fromyear-1;endif;return(V_birth);endGetage;functiongetSalary(VFpsncodevarchar)returnnumber--返回工資情況isV_psnSalarynumber(8,2);beginSelectFpsnSalaryintoV_psnSalaryfromT_PsnSalarywhereFpsncode=VFpsncode;return(V_psnSalary);endgetSalary;endpackage_demo;selecta.*,package_demo.Getage(Fpsnbirth,'')agefromT_psnsalarya;--調用包得到年齡功能selectpackage_demo.getsalary('C001')fromdual;--代碼得到工資