ORACLE 游標遍歷

2021-08-31 19:43:49 字數 2344 閱讀 5047

-- created on 2011-10-9 by huchangkun

declare

-- local variables here

cursor c_event is select t.eventname from t_event t;

temp t_event.eventname%type;

begin

-- for 遍歷游標

for temp in c_event

loop

dbms_output.put_line('事件名稱:'|| temp.eventname);

end loop;

dbms_output.put_line('------------------分割線--------------');

--while 遍歷游標

open c_event;--開啟游標

fetch c_event into temp; --取值

while c_event%found

loop

dbms_output.put_line('事件名稱'||temp); --列印

fetch c_event into temp; --取值

end loop;

close c_event;

dbms_output.put_line('------------------分割線--------------');

--loog 遍歷游標

open c_event;--開啟游標

loop

fetch c_event into temp; --取值

exit when c_event%notfound;

dbms_output.put_line('事件名稱'||temp); --列印

end loop;

close c_event;

end;

注意:必須先fetch,再用光標的notfound來判斷

---帶入參的游標,注意只指定入參型別,不指定入參長度,且只有入參,無出參

-- created on 2011-10-9 by huchangkun

declare

-- local variables here

cursor c_event(row_num number default 3) is (select t.eventname from t_event t where rownum 2)

loop

dbms_output.put_line('事件名稱:'|| temp.eventname);

dbms_output.put_line('index: '||c_event%rowcount);

end loop;

dbms_output.put_line('------------------分割線--------------');

--while 遍歷游標

open c_event(row_num=>3);--開啟游標

fetch c_event into temp; --取值

while c_event%found

loop

dbms_output.put_line('事件名稱:'||temp); --列印

dbms_output.put_line('index: '||c_event%rowcount);

fetch c_event into temp; --取值

end loop;

close c_event;

dbms_output.put_line('------------------分割線--------------');

--loog 遍歷游標

open c_event(row_num=>4);--開啟游標

loop

fetch c_event into temp; --取值

exit when c_event%notfound;

dbms_output.put_line('事件名稱:'||temp); --列印

dbms_output.put_line('index: '||c_event%rowcount);

end loop;

close c_event;

end;

[b]注意捕獲異常:[/b]

dbms_output.put_line(sqlcode||' '||sqlerrm);

sqlcode是錯誤**,sqlerrm是錯誤資訊,不是sql語句本身,我要的是發生錯誤的sql語句

exception

when others then

dbms_output.put_line(sqlcode||' '||sqlerrm);

ORACLE游標遍歷

oracle游標遍歷 created on 2011 10 9 by huchangkun declare local variables here cursor c event is select t.eventname from t event t temp t event.eventname ...

mysql 遍歷游標 處理

1,建立儲存過程 delimiter use test drop procedure if exists proc style10 t use test create procedure test.proc style10 t begin declare v cmp id int declare v...

sql 游標迴圈遍歷

原文 sql 游標迴圈遍歷 寫儲存過程的時候碰到乙個需要對資料進行遍歷迴圈操作的問題,最後通過游標解決了,感覺很適用 1 declare level varchar 100 2 declare uid varchar 100 3 declare cur cursor 定義乙個游標 4read onl...