oracle 建立過程與函式小記

2021-08-07 21:19:57 字數 4084 閱讀 8276

---恢復內容開始---

--儲存過程

--案例1:

編寫乙個過程,輸入員工姓名,和乙個新工資,將emp表中的該員工的工資改為新工資

--1.結構:create or replace procedure 過程名(引數列表)is/as begin 執行語句 end;

--2.引數列表:name,newsal是乙個形參。注意:不能給形參乙個長度

--3.過程的使用:

create or replace procedure updatesal(name varchar2,newsal number)

asbegin

update emp

set sal=newsal where ename=name;

end;

--案例2:

建立乙個過程,輸入乙個員工編號,判斷該員工的工資是否小於2000,如果小於2000,那麼給他的工資加上500,通過引數,將新工資給傳遞出來

--1.in:表示是輸入引數,out:表示輸出引數,in out:表示輸入輸出引數

--2.在儲存過程中,不能用declare關鍵字

--3.需要宣告區域性變數的時候結構:create or replace procedure 過程名(引數列表)is 區域性變數的宣告 begin 執行語句 end;

--4.如果沒有寫in,out,in out的話,預設的是輸入引數。

create or replace procedure getnewsal(spno in number,newsal out

number)

isv_sal emp.sal%type;

begin

select sal into v_sal from emp where empno=spno;

if v_sal<2000

then

update emp

set sal=sal+500

where empno=spno;

end if;

select sal into newsal from emp where empno=spno;

end;

--案例3:

編寫過程,輸入乙個部門編號,顯示該部門員工的姓名,工資,入職日期

--1

.儲存過程中可以定義普通變數,還可以定義游標變數

create or replace procedure sp_pro5(spno number)

as--宣告乙個游標變數

cursor v_cursor

isselect ename,sal,hiredate from emp where deptno=spno;

v_ename emp.ename%type;

v_sal emp.sal%type;

v_date emp.hiredate%type;

begin

for v_cursors in

v_cursor loop

v_ename:=v_cursors.ename;

v_sal:=v_cursors.sal;

v_date:=v_cursors.hiredate;

dbms_output.put_line(

'姓名:

'||v_ename ||'

工資:'||v_sal||'

入職日期:

'||v_date);

end loop;

end;

--執行過程

declare

v_deptno emp.deptno%type:=&no;

begin

sp_pro5(v_deptno);

end;

--刪除儲存過程

--drop procedure 過程名

eg:

drop procedure sp_pro1;

--函式:

--案例4:

建立乙個函式,輸入乙個員工編號,判斷該員工的工資是否小於2000,如果小於2000,那麼給他的工資加上500,返回新工資。

--總結:1.結構:create [or replace] function 函式名(引數列表) return 返回值的型別 is|as 區域性變數 begin 執行語句 end;

--2.在執行語句中,需要有乙個return語句,來返回乙個值。

--3.在用函式的時候,返回乙個值。如果確實需要獲得多個值,一般使用儲存過程,通過引數列表中的out引數獲得(輸出引數)

create or replace function getempnewsal(spno in number) return

number

is--定義乙個區域性變數

v_sal emp.sal%type;

v_newsal emp.sal%type;

begin

select sal into v_sal from emp where empno=spno;

if v_sal<2000

then

update emp

set sal = sal+500

where empno=spno;

end if;

select sal into v_newsal from emp where empno=spno;

return

v_newsal;

end;

--呼叫函式

declare

v_empno emp.empno%type:=&no;

v_newsal emp.sal%type;

begin

v_newsal:=getempnewsal(v_empno);

dbms_output.put_line(v_newsal);

end;

--案例5:

建立乙個函式,輸入乙個員工編號,判斷該員工的工資是否小於2000,如果小於2000,工資加上500,否則加200,返回新工資。

--return:函式中可以有多個return語句,但是只返回一次的。執行return語句,函式將執行結束並返回結果

create or replace function getempnewsal(spno in number) return

number

is--定義乙個區域性變數

v_sal emp.sal%type;

v_newsal emp.sal%type;

begin

select sal into v_sal from emp where empno=spno;

if v_sal<2000

then

update emp

set sal = sal+500

where empno=spno;

select sal into v_newsal from emp where empno=spno;

return

v_newsal;

else

update emp

set sal = sal+200

where empno=spno;

select sal into v_newsal from emp where empno=spno;

return

v_newsal;

end if;

end;

--刪除函式:

drop function 函式名

eg:

drop function sp_func1;

---恢復內容結束---

Oracle儲存過程小記

oracle儲存過程小記 dual 在oracle中,我們有時候會需要判斷乙個字串裡邊是否包含有某乙個串 首先,oracle為我們提供了instr這個函式 instr string1,string2 start position nth appearance 引數分析 string1,源字串,要在此...

Oracle 建立函式與儲存過程語句積累

1.建立乙個返回字串的函式 create or replace function get hello msg return varchar2 as begin return hello world end get hello msg 檢視函式的型別和狀態 select object name,obj...

Oracle建立儲存過程 建立函式 建立包

一 oracle建立儲存過程 1 基本語法 create orreplace procedureupdate emp sal name inout type,name inout type,is begin endupdate emp sal 2 寫乙個簡單的例子修改emp表的ename欄位 cre...