OraclePL SQL物件導向之package

2021-06-07 11:22:15 字數 3803 閱讀 5481

將方法和過程用包定義

create

or replace package pkg_emp

as
--輸入員工編號查詢出員工資訊
procedure pro_findinfo(
in_empno emp2.empno%type,
out_name out emp2.ename%type,
out_sal out emp2.sal%type
);
--根據部門編號修改本部門員工工資
procedure pro_editinfo(
in_emp_record emp2%rowtype,
out_flag out

boolean

);
--輸入新員工資訊並儲存到資料庫
procedure pro_addinfo(
in_emp_new_record emp2%rowtype
);
--統計工資資訊
function fun_sum(
num_a number,
num_b number
) return number;
end pkg_emp;
--實現包
create

or replace package body pkg_emp

as
--輸入員工編號查詢出員工資訊
procedure pro_findinfo(
in_empno emp2.empno%type,
out_name out emp2.ename%type,
out_sal out emp2.sal%type
)
as
begin
select ename, sal into out_name, out_sal from emp2 where empno = in_empno;
end pro_findinfo;
--根據部門編號修改本部門員工工資
procedure pro_editinfo(
in_emp_record emp2%rowtype,
out_flag out

boolean

)
is
begin
update emp2 set sal = in_emp_record.sal where deptno = in_emp_record.deptno;
out_flag := true;
/*exception
when no_data_found then
out_flag := false;
commit;*/
if (sql%rowcount

< 1) then

out_flag := false;
else
out_flag := true;
commit;
end

if;

end pro_editinfo;
--輸入新員工資訊並儲存到資料庫
procedure pro_addinfo(
in_emp_new_record emp2%rowtype
)
as
temp_sql varchar2(200);
begin
temp_sql := 'insert into emp2(empno, ename, sal, comm, deptno) values(:1, :2, :3, :4, :5)';
execute

immediate temp_sql using in_emp_new_record.empno, in_emp_new_record.ename,

in_emp_new_record.sal, in_emp_new_record.comm, in_emp_new_record.deptno;
commit;
end;
--統計工資資訊
function fun_sum(
num_a number,
num_b number
) return number
is
begin
return num_a + num_b;
end fun_sum;
end pkg_emp;
--測試1
declare
out_name emp2.ename%type;
out_sal emp2.sal%type;
begin
pkg_emp.pro_findinfo(7369, out_name, out_sal);
dbms_output.put_line(out_name);
dbms_output.put_line(out_sal);
end;
--測試2
select * from emp2;
declare
in_emp_record emp2%rowtype;
flag boolean;
begin
in_emp_record.deptno := &部門編號;
in_emp_record.sal := &員工工資;
pkg_emp.pro_editinfo(in_emp_record, flag);
if (flag = false) then
dbms_output.put_line('no');
else
dbms_output.put_line('yes');
end

if;

end;
--測試3
declare
new_emp_record emp2%rowtype;
begin
new_emp_record.empno := &員工編號;
new_emp_record.ename := &姓名;
new_emp_record.sal := &工資;
new_emp_record.comm := &獎金;
new_emp_record.deptno := &部門編號;
pkg_emp.pro_addinfo(new_emp_record);
end;
--測試4
declare
sum_emp number;
begin
select pkg_emp.fun_sum(sal, nvl(comm, 0)) into sum_emp from emp2
where empno = &員工編號;
dbms_output.put_line('員工總工資:' || sum_emp);
end;

物件導向 初識物件導向

面向過程思想 步驟清晰簡單,第一步做什麼,第二步做什麼.面向過程適合處理一些較為簡單的問題 物件導向思想 物以類聚,分類的思維模式,思考問題首先會解決問題需要分哪些類,然後對這些類進行單獨思考,最後才是對某個分類下的細節進行面向過程的思索 物件導向適合處理複雜的問題,適合處理需要多人協作的問題 對於...

物件導向程式設計01 面向過程 物件導向

前面12講我已經寫完了從零開始學j ase 慶祝完結撒花!那麼從今天開始正式步入物件導向程式設計。建議開啟本章之前先回顧 j ase010方法詳解和 j a變數與方法的呼叫 同類操作中與跨類操作中的對比 物件導向程式設計 oop object oriented programming 物件導向程式設...

物件導向過程與物件導向

物件導向過程與物件導向 1 程式的發展經歷了兩個階段 面向過程 物件導向。2 對於物件導向與面向過程可以用乙個例子解釋,如乙個木匠要做乙個盒子,那麼這個盒子的出發點會有兩種方式 物件導向 先想好要做的盒子,之後在去找相應的工具去做。面向過程 不去想要做什麼樣的盒子,隨需取工具。物件導向三大特徵 封裝...