Oracle帶輸入輸出引數的儲存過程

2021-07-16 15:05:32 字數 3111 閱讀 4338

(一)使用輸入引數

需求:在emp_copy中新增一條記錄,empno為已有empno的最大值+1,ename不能為空且長度必須大於0,deptno為60。

建立儲存過程:

create or replace procedure insert_emp(emp_name in varchar2, dept_no in number) as 

begin

declare max_empno number;

begin

if(emp_name is null or length(emp_name) = 0) then

return;

end if;

if(dept_no != 60) then

return;

end if;

select max(empno) into max_empno from emp_copy;

insert into emp_copy(empno, ename, deptno) values(max_empno + 1, emp_name, dept_no);

end;

end insert_emp;

/procedure created.

呼叫儲存過程並驗證:

(1)sql>execute insert_emp('li si', 60);

pl/sql procedure successfully completed.

sql>

col empno format 99999;

col ename format a15;

col deptno format 99999;

select empno, ename, deptno from emp_copy where deptno = 60;

empno ename           deptno

------ --------------- ------

7981 li si           60

(2)sql> execute insert_emp('', 6);

pl/sql procedure successfully completed.

sql> select empno, ename, deptno from emp_copy where deptno = 6;

sql> 

(二)使用輸出引數

需求:在上個需求的基礎上,要分別統計表emp_copy插入資料前後的記錄數。

建立儲存過程:

create or replace procedure insert_emp(emp_name in varchar2, dept_no in number, original_count out number, current_count out number) as 

begin

declare max_empno number;

begin

if(emp_name is null or length(emp_name) = 0) then

return;

end if;

if(dept_no != 60) then

return;

end if;

select count(1) into original_count from emp_copy;

select max(empno) into max_empno from emp_copy;

insert into emp_copy(empno, ename, deptno) values(max_empno + 1, emp_name, dept_no);

select count(1) into current_count from emp_copy;

end;

end insert_emp;

/procedure created.

呼叫儲存過程:

declare count1 number;

count2 number;

begin

insert_emp('wang wu', 60, count1, count2);

dbms_output.put_line('original count of table emp_copy is ' || count1);

dbms_output.put_line('current count of table emp_copy is ' || count2);

end;

/original count of table emp_copy is 15

current count of table emp_copy is 16

pl/sql procedure successfully completed.

(三)使用輸入輸出引數

in out引數綜合了上述兩種引數型別,既向過程體傳值,也被賦值而傳到過程體外。in out引數既可以用作輸入也可以用作輸出。

需求:實現兩數交換。

建立儲存過程:

create or replace procedure swap(value1 in out number, value2 in out number) as

begin

value1 := value1 + value2;

value2 := value1 - value2;

value1 := value1 - value2;

end swap;

/procedure created.

呼叫儲存過程:

declare a number := 22;

b number := 33;

begin

dbms_output.put_line('before swap: a = ' || a || ', b = ' || b);

swap(a, b);

dbms_output.put_line('after swap: a = ' || a || ', b = ' || b);

end;

/before swap: a = 22, b = 33

after swap: a = 33, b = 22

pl/sql procedure successfully completed.

Oracle 10g建立函式 帶輸入 輸出引數

oracle10g 開發函式 1 建立不帶引數的函式 create or replace function get user return varchar2 isv user varchar2 100 begin select username into v user from user users...

C 呼叫帶輸入輸出引數及結果集Oracle儲存過程

資料庫儲存過程如圖 winfrom後端 引用using oracle.manageddataaccess.client public datatable getcommaddecsdata string opptype,string data par 0 value opptype par 1 va...

關於matlab的輸入輸出引數

nargin nargin為 number of input arguments 的縮寫。在matlab中定義乙個函式時,在 函式體內部,nargin是用來判斷輸入變數個數的函式。nargin fx 在函式fx的定義宣告中返回輸入引數的個數。如果函式包括varargin其定義,那麼nargin返回的...