本文實例講述了Oracle存儲過程游標用法。分享給大家供大家參考,具體如下:
使用游標的5個步驟
1、聲明一些變量用于保存select語句返回的指
2、聲明游標,并指定select 語句
3、打開游標
4、從游標中獲取記錄
5、關閉游標
從游標中獲取每一條記錄可使用fetch語句。fetch語句將列的指讀取到指定的變量中;
語法:
1
2
|
fetch cursor_name into variable[, variable ...]; |
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
create or replace procedure sel_person is v_id person.id%type; v_name person.pname%type; v_birthday person.birthday%type; cursor temp_cursor is select * from person; begin open temp_cursor; loop fetch temp_cursor into v_id,v_name,v_birthday; exit when temp_cursor%notfound; dbms_output.put_line(v_id|| '----' ||v_name|| '----' ||v_birthday); end loop; close temp_cursor; end sel_person; |
備注:為了確定循環是否結束,可以使用布爾變量temp_cursor%notfound。當fetch達到游標中最后一條記錄,不能再讀取更多記錄的時候,這個變量就為真。
希望本文所述對大家Oracle程序設計有所幫助。