儲存過程引數模式包括in、out、 in out。
in(預設引數模式):表示當儲存過程別呼叫時,實參值被傳遞給形參;形參起變數作用,只能讀該引數,而不能修改該引數。in模式引數可以是變數或表示式。
out:表示當儲存過程被呼叫時,實參值被忽略;形參起未初始化的pl/sql變數的作用,形參的初始值為null,可以進行讀/寫操作,在儲存過程呼叫結束後,形參值被給實參。out
模式引數只能是變數,不能是常量或表示式。
in out表示當儲存過程被呼叫時,形參值被傳遞給形參。形參起已初始化的pl/sql變數的作用,可讀可寫。in out 模式引數只能是變數,不能是常量或表示式。
使用out、in out模式引數時只有當程式正常結束時形參值才會傳遞給實參。
舉例:
create or replace procedure proc_divide
(num1 in out number,num2 in out number) is
r1 number;
r2 number;
begin
r1:=trunc(num1/num2);
r2:=mod(num1,num2);
num1 := r1;
num2 := r2;
exception
when zero_divide then
dbms_output.put_line('除法中分母不能為零');
when others then
dbms_output.put_line('程式執行錯誤!請使用游標');
end proc_divide;
set serveroutput on
declare
n1 number:=&n1;
n2 number:=&n2;
begin
proc_divide(n1,n2);
dbms_output.put_line('兩個數字相除的結果是:'||n1);
dbms_output.put_line('取餘的結果是:'||n2);
end;
(1)函式的建立與儲存過程的建立相似,不同之處在於,函式有乙個顯示的返回值。
(2)在函式的建立過程中沒有declare關鍵字,而是使用is或者as關鍵字來代替。
(3)函式必須有返回值:return datatype。
(4)一般不在函式中執行 dml(資料操縱語言-插入、刪除、更新)操作。
舉例:儲存過程呼叫函式
--函式 increasesalary()
create or replace function increasesalary(theincome in number) return varchar2
as
themessage varchar2(50);
begin
if theincome <=4000 then
themessage := '收入太低,工資增加10%';
elsif theincome <=8000 then
themessage := '收入偏低,工資增加5%';
elsif theincome <=20000 then
themessage := '收入一般,工資增加2%';
else
themessage := '收入很高,工資增加1%';
end if;
return themessage;
end;
--儲存過程
create or replace procedure getempinfo
(theid in out employees.employee_id%type,
thename out employees.first_name%type,
thesalary out employees.salary%type,
themessage out varchar2)
isbegin
select employee_id,first_name,salary,increasesalary(salary)
into theid,thename,thesalary,themessage
from employees
where employee_id = theid;
exception
when no_data_found then
dbms_output.put_line('沒有該員工資訊');
end;
set serveroutput on ;
declare
theid employees.employee_id%type:=&theid;
thename employees.first_name%type;
thesalary employees.salary%type;
themessage varchar2(50);
begin
getempinfo(&theid,thename,thesalary,themessage);--輸入員工id
dbms_output.put_line('id為:'||theid||'的員工,名字為'||thename
||', 收入為'||thesalary||','||themessage);
end;
Oracle儲存函式,儲存過程
一 oracle儲存函式 儲存的pl sql語法塊,完成特定的功能。1 語法 函式關鍵字 function 1 建立函式 create or replace function function name param1,param2 return is as local declarations de...
Oracle 儲存過程 函式
儲存過程 函式 子程式有名字 儲存在資料庫 同過名字呼叫 建立的時候被編譯 可以被其他有名或者無名的塊呼叫 語法create or replace procedure testp1 as 宣告 begin 可執行 dbms output.put line hello world end 使用 exe...
Oracle 儲存過程 函式
儲存過程和函式在oracle中被稱為子程式,是指被命名的pl sql塊,這種塊可以帶有引數,可以被多次呼叫。儲存過程用於執行特定操作,而函式則用於返回特定的資料。儲存過程是儲存在資料庫中的有名字的pl sql程式塊,接受零個或多個引數作為輸入 input 或輸出 output 或既作輸入又作輸出 i...