oracle儲存過程

2021-10-01 16:45:46 字數 4193 閱讀 8688

儲存過程,它是乙個有名字的plsql**塊

建立之後會儲存到資料庫中

當資料庫啟動時,會自動載入到資料庫記憶體中,執行效率高

儲存過程有引數,沒有返回值,但是有輸出引數

儲存過程的建立語法

create [or replace] procedure 儲存過程名

[(形參 [in|out|in out] 資料型別,...)]

is|as

宣告部分

begin

plsql**塊

exception

異常處理部分

end;

建立乙個儲存過程,列印10部門員工資訊

create or replace procedure p

isbegin

for v in (select * from emp where deptno=10) loop

dbms_output.put_line(v.empno||' , '||v.ename||' , '||v.job||' , '||v.deptno);

end loop;

end;

語法

儲存過程名[(實參)];
begin

p();

end;

輸出

9123 , june ,  , 10

7782 , clark , manager , 10

7839 , king , president , 10

7934 , miller , clerk , 10

語法

call 儲存過程名(實參)
call p();
輸出

9123 , june ,  , 10

7782 , clark , manager , 10

7839 , king , president , 10

7934 , miller , clerk , 10

語法

exec 儲存過程名(實參)
sql> set serveroutput on

sql> exec p();

9123 , june , , 10

7782 , clark , manager , 10

7839 , king , president , 10

7934 , miller , clerk , 10

pl/sql 過程已成功完成。

儲存過程的引數

傳值方式

in引數:表示輸入引數,可以使用任意一種傳參方式

in引數實參的值只能被使用,不能被修改

注意,通過in引數傳的值,不能在程式中修改eno變數中的值

建立乙個儲存過程,傳入員工編號,列印員工資訊

create or replace procedure p1(eno in emp.empno%type)

is --宣告乙個變數,儲存員工資訊

v emp%rowtype;

begin

--根據員工編號查詢員工資訊

select * into v from emp where empno=eno;

--列印員工資訊

dbms_output.put_line(v.empno||' , '||v.ename||' , '||v.job||' , '||v.deptno);

exception

when no_data_found then

dbms_output.put_line('員工不存在');

end;

呼叫

傳值的方式呼叫p1

begin

p1(7369);

end;

輸出

7369 , smith , clerk , 20
傳變數的方式呼叫

declare

empno number(4):=7499;

begin

p1(empno);

end;

輸出

7499 , allen , salesman , 30
形參=>值的方式呼叫

begin

p1(eno=>7521);

end;

輸出

7521 , ward , salesman , 30
out引數: 輸出引數,它只能以傳變數的方式傳值,

out引數的值是可以修改的

將儲存過程的執行結果放到變數中,通過變數將結果傳給外部程式

建立乙個儲存過程

輸入乙個員工編號,查詢員工資訊,並將查詢結果傳給呼叫程式

create or replace procedure p2(eno in emp.empno%type,

v out emp%rowtype)

isbegin

select * into v from emp where empno=eno;

exception

when no_data_found then

dbms_output.put_line('員工不存在');

end;

呼叫

declare

v1 emp%rowtype;

begin

p2(7369,v1);

dbms_output.put_line(v1.empno||' , '||v1.ename||' , '||v1.job||' , '||v1.deptno);

end;

輸出

7369 , smith , clerk , 20
建立乙個儲存過程計算乙個數字的階乘

create or replace procedure p3(n in number,res out number)

isbegin

--將res初始值設定為1

res:=1;

for i in 1..n loop

res:=res*i;

end loop;

end;

呼叫

declare

n number(10);

begin

p3(6,n);

dbms_output.put_line(n);

end;

輸出

720
in out引數:輸入輸出引數,它擁有in和out的所有特性

必須以傳變數的方式傳參,傳入引數的初始值是有意義的,值是可以修改的

建立乙個儲存過程,傳入員工編號,查詢員工資訊,傳給外部程式

create or replace procedure p4(v in out emp%rowtype)

isbegin

select * into v from emp where empno=v.empno;

end;

呼叫

declare

v1 emp%rowtype;

begin

v1.empno:=7369;

p4(v1);

dbms_output.put_line(v1.empno||' , '||v1.ename||' , '||v1.job||' , '||v1.deptno);

end;

輸出

7369 , smith , clerk , 20

Oracle儲存過程呼叫儲存過程

oracle儲存過程呼叫有返回結果集的儲存過程一般用光標的方式,宣告乙個游標,把結果集放到游標裡面,然後迴圈游標 declare newcs sys refcursor cs1 number cs2 number cstype table rowtype table列的個數和newcs返回的個數一樣...

ORACLE儲存過程

自定義函式開始 create or replace function fn wftemplateidget templatecategoryid number,organid number,templatemode number return number istemplateid number i...

Oracle 儲存過程

create or replace procedure p 有就替換,沒有就建立 iscursor c is select from emp for update begin for v emp in c loop if v emp.deptno 10 then update emp2 set sa...