oracle游標,異常,儲存過程用法

2022-09-03 02:54:07 字數 4494 閱讀 8967

在專案裡寫了個小的存數過程,功能是給一些特定人員型別補錄對應檔案流水號的。總結下:

開頭:create or replace procedure   insert_serial   as

as後邊接申請的變數:  

pernum_str   person_info.person_no%type;   表示宣告的變數 pernum_str   的型別 和 person_info.person_no一樣

cursor cur_perinfo is                       表示定義乙個游標cursor     

select person_no, person_type,hj_area_id   表示游標 對應這個查詢結果集

from person_info;

create

orreplace

procedure insert_serial as

pernum_str person_info.person_no

%type;

pertype_str person_info.person_type

%type;

perorgan_str person_info.hj_area_id

%type;

licnum_num licence_serial_no.cur_num

%type;

lictype_str licence_serial_no.licence_type

%type;

cursor cur_perinfo is

select

person_no, person_type,hj_area_id

from person_info;

開始遍歷結果集合:

首先要宣告開啟游標,讀資料要用fetch語句完成,依次把每行結果集放入到已經宣告的變數裡。

如果顯式的開啟游標,一定要加上 %found 屬性判斷,否則當游標到最後一行時候,就會無限迴圈取出最後一行結果集,不會自動推出loop迴圈

最後end loop 迴圈,提交事務commit,關閉游標

也可以用 for..in..:

用for....in... 語句遍歷結果集  則不需要宣告開啟游標。(變數v後邊直接接欄位名,就可以取出結果)

/*

begin

for v in cur_perinfo

loop

dbms_output.put_line(v.person_no);

end loop;

*/

對游標取出的結果集合進行處理:

如果游標沒有有效資料了,跳轉到 exit 推出迴圈。

select .... into ... from   隱式游標,表示把查詢出來的結果放入到對應的變數裡。這種寫法每次只能查出一行資料,如果結果有多行(too_many_rows)或者沒有資料查出(no_data_found),程式會丟擲異常,中斷迴圈。

所以新增了異常處理 begin....exception  (when then end;):

使用了 goto 跳轉 ,當出現異常了,跳到<>,然後繼續迴圈。

在oracle中 「 :=「 表示賦值,「=」表示判斷等價。 「||」 表示字串拼接

使用to_char() 對數字格式化的時候,會自動把轉換格式後的字元最高位,來儲存數字符號,所以當正數格式化的時候,最高位會多出個空格。可以「fm」來消除空格

if cur_perinfo%found then

begin

select nvl(cur_num, -

1), licence_type

into

licnum_num, lictype_str

from

licence_serial_no

where area_id =

pertype_str

and cur_area_id =

perorgan_str;

exception

when no_data_found then

goto

point1;

when too_many_rows then

goto

point1;

end;

if licnum_num >-1

then

licnum_num :

= licnum_num +1;

update

licence_serial_no

set cur_num =

licnum_num

where area_id =

pertype_str

and cur_area_id =

perorgan_str;

update

person_info

set document_no =(lictype_str||to_char(licnum_num,'

fm0999'))

where person_no =

pernum_str;

endif

;

<>

null

;

else

exit

;

endif;

完整**:

create

orreplace

procedure insert_serial as

pernum_str person_info.person_no

%type;

pertype_str person_info.person_type

%type;

perorgan_str person_info.hj_area_id

%type;

licnum_num licence_serial_no.cur_num

%type;

lictype_str licence_serial_no.licence_type

%type;

cursor cur_perinfo is

select

person_no, person_type,hj_area_id

from

person_info;

/*begin

for v in cur_perinfo loop

dbms_output.put_line(v.person_no);

end loop;

*/begin

open

cur_perinfo;

loop

fetch

cur_perinfo

into

pernum_str, pertype_str, perorgan_str;

if cur_perinfo%found then

begin

select nvl(cur_num, -

1), licence_type

into

licnum_num, lictype_str

from

licence_serial_no

where area_id =

pertype_str

and cur_area_id =

perorgan_str;

exception

when no_data_found then

goto

point1;

when too_many_rows then

goto

point1;

end;

if licnum_num >-1

then

licnum_num :

= licnum_num +1;

update

licence_serial_no

set cur_num =

licnum_num

where area_id =

pertype_str

and cur_area_id =

perorgan_str;

update

person_info

set document_no =(lictype_str||to_char(licnum_num,'

fm0999'))

where person_no =

pernum_str;

endif

;

<>

null

;

else

exit

;

endif;

endloop;

commit;

close

cur_perinfo;

end insert_serial;

oracle儲存過程,游標

oracle儲存過程,游標 2010 07 07 13 01 create or replace procedure p tb task log is 功能 插入任務到任務日誌表 v task start date date v task end date date v sql code numbe...

oracle 儲存過程 游標

create or replace procedure exception3 as 使用者自定義異常 e too high sal exception 宣告自定義異常 v sal employees.salary type begin select salary into v sal from em...

oracle儲存過程 游標篇

oracle 中cursor用於遍歷臨時表中的查詢結果 1 cursor 型游標 不能用於引數傳遞 create or replace procedure test is cusor 1 cursor is select std name from student where cursor 的使用方...