Oracle PL SQL 儲存過程

2021-07-27 07:01:24 字數 2054 閱讀 8804

1.oracle 提供可以吧pl/sql程式儲存在資料庫中,並且可以在任何地方來運用它。這樣就叫儲存過程或者函式。

2.建立函式

例:返回helloworld的函式,is相當於declare用於宣告區域性變數,第乙個return只宣告返回型別

create or replace function hello_world

return vaarchar2

isbegin

return 'helloworld';

end;

呼叫該函式

select  hello_world from dual;

例2:建立帶引數的函式

create or replace function hello_world(v_logo varchar2)

return varchar2

isbegin

return 'helloworld'||v_logo;

endl;

呼叫該函式

select  hello_world('testlogo') from dual;

例3:返回當前系統時間的儲存函式

create or replace funtion get_sysdate

return date

isv_date date;

begin

v_date :=sysdate;

return v_date;

end;

例4:定義乙個函式,獲取給定部門的工資總和,要求:部門編號為引數,工資總和為返回值

create or replace funtionc get_sumsal(dept_id number)

return number

isv_sumsal number(10) :=0;

cursor salary_cursor is select salary from employees where deptment_id=dept_id;

begin

for c in salary_cursor loop

v_sumsal :=v_sumsal+c.salary;

end loop;

return v_sumsal;

end;

3.關於out型的引數

因為函式只能有乙個返回值,pl/sql程式可以通過out型的引數實現有多個返回值。

例:定義乙個函式,獲取給定部門的工資總和,要求:部門編號為引數,工資總和為返回值,員工總數用out型別

create or replace funtionc get_sumsal(dept_id numbertotal_count out number)

return number

isv_sumsal number(10) :=0;

cursor salary_cursor is select salary from employees where deptment_id=dept_id;

begin

total_count :=0;

for c in salary_cursor loop

v_sumsal :=v_sumsal+c.salary;

total_count :=total_count+1;

end loop;

return v_sumsal;

end;

呼叫該函式: 使用plsql呼叫,建立v_num變數,引入out位置。函式會在執行中賦值。

declare

v_num number(5);

begin

dbms_output.out_line(get_sumsal(80,v_num));

dbms_output.out_line(v_num);

end;

OraclePL SQL儲存過程

create or replace 建立或替換,如果存在就替換,不存在就建立create or replace procedure piscursor cisselect from dept2 for update beginfor row record in c loopif row record...

oracle plsql開發 儲存過程綜合練習

基於表emp和dept 構造procedure change salary 引數 ename in varchar2 salary in number v job out varchar2 v dname out varchar2 先查詢指定員工,如果查出多條記錄,提示並異常退出 如果沒有該名員工,...

oracle pl sql之oracle儲存過程

儲存過程是一種命名pl sql程式塊,它可以被賦予引數,儲存在資料庫中,可以被使用者呼叫。由於儲存過程是已編譯好的 所以在呼叫的時候不必再次進行編譯,從而提高了程式的執行效率。另外使用儲存過程可以實現程式的模組化設計 儲存過程的語法 create or replace procedure proce...