Oracle 存儲過程和存儲函數

一、存儲過程

存儲過程(Stored Procedure)是在大型數據庫系統中,一組爲了完成特定功能的 SQL 語句集,經
編譯後存儲在數據庫中,用戶通過指定存儲過程的名字並給出參數(如果該存儲過程帶有參數)來
執行它

存儲過程是數據庫中的一個重要對象,任何一個設計良好的數據庫應用程序都應該用到存儲過程。

存儲過程:存儲過程就是提前已經編譯好的一段pl/sql語言,放置在數據庫端
可以直接被調用,這一段pl/sql一般都是固定步驟的業務。

創建過程語法:

create [or replace] PROCEDURE 過程名[(參數名 in/out 數據類型)]
AS
begin
	PLSQL 子程序體;
End;

或者:
create [or replace] PROCEDURE 過程名[(參數名 in/out 數據類型)]
is
begin
	PLSQL 子程序體;
End 過程名;
  • 創建一個輸出 helloword 的存儲過程
    在這裏插入圖片描述
  • 範例 2:給指定的員工漲 100 工資
    分析:我們需要使用帶有參數的存儲過程
create or replace procedure p1(eno emp.empno%type)
is
begin
   update emp set sal=sal+100 where empno = eno;
   commit;
end;

select * from emp where empno = 7788;
----測試p1
declare

begin
  p1(7788);
end;

二、存儲函數

存儲過程和存儲函數的區別:
一般來講,過程和函數的區別在於函數可以有一個返回值;而過程沒有返回值。
但過程和函數都可以通過 out 指定一個或多個輸出參數。我們可以利用 out 參數,在過程和函數中實現返回多個值。

在這裏插入圖片描述

  • 範例:使用存儲函數來查詢指定員工的年薪
    在這裏插入圖片描述
使用存儲過程來替換上面的例子
create or replace procedure empincomep(eno in emp.empno%type,
income out number) is
	psal emp.sal%type;
	pcomm emp.comm%type;
begin
	select t.sal, t.comm into psal, pcomm from emp t where t.empno = eno;
	income := psal*12+nvl(pcomm,0);
end empincomep;

調用:
declare
	income number;
begin
	empincomep(7369, income);
	dbms_output.put_line(income);
end;
----通過存儲函數實現計算指定員工的年薪
----存儲過程和存儲函數的參數都不能帶長度
----存儲函數的返回值類型不能帶長度
create or replace function f_yearsal(eno emp.empno%type) return number
is
  s number(10);     
begin
  select sal*12+nvl(comm, 0) into s from emp where empno = eno;
  return s;
end;

----測試f_yearsal
----存儲函數在調用的時候,返回值需要接收。
declare
  s number(10); 
begin
  s := f_yearsal(7788);
  dbms_output.put_line(s);
end;

在這裏插入圖片描述

三、in 和 out 類型參數

凡是涉及到 into 查詢語句賦值或者:=賦值操作的參數,都必須使用out來修飾

--使用存儲過程來算年薪
create or replace procedure p_yearsal(eno emp.empno%type, yearsal out number)
is
   s number(10);
   c emp.comm%type;
begin
   select sal*12, nvl(comm, 0) into s, c from emp where empno = eno;
   yearsal := s+c;
end;

---測試p_yearsal
declare
  yearsal number(10);
begin
  p_yearsal(7788, yearsal);
  dbms_output.put_line(yearsal);
end;

四、存儲過程和存儲函數的區別

  • 語法區別:關鍵字不一樣,
    存儲函數比存儲過程多了兩個return。

  • 本質區別:存儲函數有返回值,而存儲過程沒有返回值。
    如果存儲過程想實現有返回值的業務,我們就必須使用out類型的參數。
    即便是存儲過程使用了out類型的參數,起本質也不是真的有了返回值,
    而是在存儲過程內部給out類型參數賦值,在執行完畢後,我們直接拿到輸出類型參數的值。

----我們可以使用存儲函數有返回值的特性,來自定義函數。
----而存儲過程不能用來自定義函數。
----案例需求:查詢出員工姓名,員工所在部門名稱。
----案例準備工作:把scott用戶下的dept表複製到當前用戶下。
create table dept as select * from scott.dept;

----使用傳統方式來實現案例需求
select e.ename, d.dname
from emp e, dept d
where e.deptno=d.deptno;

----使用存儲函數來實現提供一個部門編號,輸出一個部門名稱。
create or replace function fdna(dno dept.deptno%type) return dept.dname%type
is
  dna dept.dname%type;
begin
  select dname into dna from dept where deptno = dno;
  return dna;
end;

---使用 fdna 存儲函數來實現案例需求:查詢出員工姓名,員工所在部門名稱。
select e.ename, fdna(e.deptno) from emp e;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章