Oracle儲存過程由例子到理論

2022-08-18 01:33:08 字數 3159 閱讀 9404

oracle hr環境新增新錶

create

table

"hr"."cus_test" (

"id"

varchar2 (255 byte) not

null

, "mobile"

varchar2 (255 byte) null

, "name"

varchar2 (255 byte) null

, "times" date

null

);alter

table "hr"."cus_test" add

check ("id" is

notnull

);alter

table "hr"."cus_test" add

primary

key ("id");

設定自增函式

create sequence autoid8 --

函式名autoid

start with2--

起始值 2

increment by2--

步長 2

minvalue 2

--最小值 2

nomaxvalue;

view code

create

orreplace

procedure vvinstat as

begin

insert

into

"hr"."cus_test" (

"id",

"mobile",

"name",

"times"

)values

( autoid.nextval,

'18575511371',

'kamil',

sysdate

) ;

commit

;end

;/

呼叫儲存過程

set serveroutput on

declare

begin

vvinstat;

end ;

create

orreplace

procedure find_emp1 (emp_no number) as emp_name varchar2 (20

) ;begin

select

ename

into

emp_name

from

emp

where

empno

=emp_no ; dbms_output.put_line (

'雇員姓名:'||

emp_name

) ; exception

when no_data_found then

dbms_output.put_line (

'雇員編號沒有找到

') ;

end ;

/

執行

set serveroutput on

declare

begin

find_emp1(

7566

);end;--

pl/sql 過程已成功完成。

--雇員姓名:jones

/*雇員編號沒有找到

*/

create

orreplace

procedure

my_proc_out (

value1

innumber

, value2 out

number

) is salary number

;begin

select

sal

into

salary

from

emp

where

empno

=value1 ;

if salary <

8000

then

value2 :

= salary +

500 ; update

emp

set sal =

value2

where

empno

=value1 ;

else

value2 :

=salary ;

endif

;

end;

/

執行:

set serveroutput on

declare v1 number :=

7566 ; v2 number

;begin

my_proc_out (v1, v2) ; dbms_output.put_line (

'v2 的值為'||

to_char (v2)

) ;end;

/

create

orreplace

procedure

test_in_out

(v1

in out number

,v2

in out number)is

value1

number:=0;

begin

value1 :

=v1;

v1 :

=v2;

v2 :

=value1;

dbms_output.put_line(

'v1:

'||v1||''

||'v2:'

||v2);

end;

/

呼叫()

set serveroutput on

declare

value1

number :=

11111

;value2

number :=

22222

;begin

test_in_out(value1,value2);

end;

/

oracle 儲存過程例子

oracle 儲存過程學習過程 建立乙個最簡單的儲存過程 create or replace procedure test xg p1 is begin dbms output.put line hello world this is the first procedure end 建立乙個帶輸入輸...

oracle儲存過程簡單例子

先建立一張表 create table mytest name varchar2 30 passwd varchar2 30 建立儲存過程 create or replace procedure sp pro1 is begin insert into mytest values jack 123 ...

Oracle 建立儲存過程例子

建立儲存過程 create or replace procedure xx p 引數in表示輸入引數,out表示輸入引數,型別可以使用任意oracle中的合法型別。is ym in char as 定義變數 vs msg varchar2 4000 錯誤資訊變數 vs ym beg char 6 起...