oracle 游標的理解

2022-09-18 19:30:11 字數 1723 閱讀 7748

①游標的狀態是通過屬性來表示。

%found :fetch語句(獲取記錄)執**況true or false。--是否指向有效行。

%notfound : 最後一條記錄是否提取出true or false。

%isopen : 游標是否開啟true or false。

%rowcount :游標當前提取的行數 。

所謂游標就是有沒有乙個箭頭指向一條記錄(自己得理解),

declare

cursor c is select * from emp;

v_b emp%rowtype;

begin

open c;

loop

fetch c into v_b;

exit when c%notfound;

p(v_b.ename||'liyang');

end loop;

close c;

end;

declare

cursor c is select * from emp;

v_b emp%rowtype;

p//是為了方便簡寫定義的乙個儲存過程就是dbms_outpue.putline的意思

begin

open c;

loop

fetch c into v_b;

p(v_b.ename||'liyang');

exit when c%notfound;

end loop;

close c;

end;

理解:上面這2個結果是不一樣的,%notfound是指游標當前指向記錄是否有資料,%found是指游標當前指向是否有資料,

為什麼後面乙個要多輸出乙個呢,是最後一步抓取的時候沒抓取成功,v_b還是上一條記錄的資料,所以會多輸出一次。

sql%isopen是乙個布林值,如果游標開啟,則為true, 如果游標關閉,則為false.對於隱式游標而言sql%isopen總是false,這是因為隱式游標在dml語句執行時開啟,結束時就立即關閉。

begin

update emp set ename='aleark' where empno=7469;

if sql%isopen then

dbms_output.put_line('openging');

else

dbms_output.put_line('closing');

end if;

if sql%found then

dbms_output.put_line('游標指向了有效行');--判斷游標是否指向有效行

else

dbms_output.put_line('sorry');

end if;

if sql%notfound then

dbms_output.put_line('also sorry');

else

dbms_output.put_line('haha');

end if;

dbms_output.put_line(sql%rowcount);

exception

when no_data_found then

dbms_output.put_line('sorry no data');

when too_many_rows then

dbms_output.put_line('too many rows');

end;

游標的理解

一 游標 用來儲存多條查詢資料的一種資料結構 結果集 它有乙個指標,用來從上往下移動,從而達到遍歷每條記錄的作用 如何編寫乙個游標?1 宣告游標 declare cursor cur name is 想要進行的操作 定義資料的方式type rowtype 2 開啟游標 open cur name 3...

ORACLE游標的應用

在oracle資料庫中,可以使用游標瀏覽資料 更新資料和刪除資料,接下來列舉以幾個簡單的例子 通過使用游標既可以逐行檢索結果集中的記錄,又可以更新或刪除當前游標行的資料如果要通過游標更新或刪除資料,在定義游標時必須要帶有for update子句其語句格式如下 cursor cursor name i...

oracle游標的使用

當select語句從資料庫中返回的記錄多餘一條時,就可以使用游標 cursor 游標可以理解為一次訪問乙個的一組記錄。select語句將列提取到游標中,然後根據游標取得記錄。使用游標時需要遵從以下的5個步驟 1 宣告一些變數,用於儲存select語句返回列值 2 宣告游標,並制定select語句 3...