Oracle儲存過程例項

2021-09-30 06:32:20 字數 2739 閱讀 7015

1.基本結構

create or replace procedure 儲存過程名字

(引數1 in number,

引數2 in number

) is

變數1 integer :=0;

變數2 date;

begin

54com.cn

end 儲存過程名字

2.select into statement

將select查詢的結果存入到變數中,可以同時將多個列儲存多個變數中,必須有一條

記錄,否則丟擲異常(如果沒有記錄丟擲no_data_found)

例子:

begin

select col1,col2 into 變數1,變數2 from typestruct where ***;

exception

when no_data_found then

***x;

end;

...3.if 判斷

if v_test=1 then

begin

do something

end;

end if;

4.while 迴圈

while v_test=1 loop

begin

***x

end;

end loop;

5.變數賦值

v_test := 123;

6.用for in 使用cursor

...is

cursor cur is select * from ***;

begin

for cur_result in cur loop

begin

v_sum :=cur_result.列名1+cur_result.列名2

end;

end loop;

end;

7.帶引數的cursor

cursor c_user(c_id number) is select name from user where typeid=c_id;

open c_user(變數值);

loop 54ne.com

fetch c_user into v_name;

exit fetch c_user%notfound;

do something

end loop;

close c_user;

/*************while迴圈*******************/

create or replace procedure stu_pro

asv_stu_name stu_info.stu_name%type;

v_stu_id stu_info.stu_id%type;

v_stu_addr stu_info.stu_addr%type;

v_count integer;

cursor stu_cur is select * from stu_info;

begin

if stu_cur%isopen then

dbms_output.put_line('stu_cur已經被人開啟了。。。');

close stu_cur;

end if;

open stu_cur;

--先fetch下

fetch stu_cur into v_stu_id,v_stu_name,v_stu_addr;

while stu_cur%found loop

if v_stu_addr='sss' then

dbms_output.put_line('------------');

update stu_info set stu_addr = 'zhang';

end if;

--迴圈中往下fetch

fetch stu_cur into v_stu_id,v_stu_name,v_stu_addr;

end loop;

commit;

close stu_cur;

end;

/******%rowcount屬性可以知道行數**********/

/**********cursor fo 迴圈*****for in *****不能顯示開啟和提取和關閉資料*********************/

/*******不帶引數的不能加括號********/

create or replace procedure stu_list

--定義異常

nonumber exception

ascursor stu_cursor is select * from stu_info;

begin

for stu_record in stu_cursor loop

dbms_output.put_line(stu_record.stu_name||'住在'||stu_record.stu_addr);

if(stu_record.stu_addr='')

end loop;

exception

when nonumber then

dbms_output.put_line('沒有數目');

--no_data_found是oracl自己內部定義的異常

when no_data_found then

dbms_output.put_line('no found record'); 

end;

Oracle儲存過程例項

1.建立儲存過程 異常處理 create or replace procedure pro test2 dmlb in varchar2,dmz in varchar2,dmsm1 in varchar2,dmsm2 in varchar2,dmsm3 in varchar2,dmsm4 in va...

Oracle儲存過程例項

簡單地講,oracle儲存過程就是儲存在oracle資料庫中的乙個程式。一.概述 oracle儲存過程開發的要點是 使用notepad文字編輯器,用oraclepl sql程式語言寫乙個儲存過程 在oracle資料庫中建立乙個儲存過程 在oracle資料庫中使用sql plus工具執行儲存過程 在o...

Oracle儲存過程例項

不帶任何引數儲存過程 create or replace procedure out date is begin dbms output.put line sysdate end out date 測試 sql set serveroutput on sql exec out date 帶in和ou...