【PL/SQL】遊標提取迴圈
注意exit when mycur%notfound; 放置的位置不同,輸出結果也不同。
第一個例子中迴圈在 it is over 提示後,又重複輸出了 owner is SYS,vid is 3;這是因為當提取到遊標結果集的最後一行之後,不再提取其他的行。所以變數 vid ,vowner 不會被改變,當再次輸出這些變數的語句時,就會重複輸出了
declare
vid t.object_id%type;
vowner t.owner%type;
vnum integer;
cursor mycur is select object_id ,owner from t where rownum <7;
begin
-- select object_id ,owner into vid,vowner from t where rownum =2;
open mycur;
loop
fetch mycur into vid,vowner;
if vnum = 6 then
dbms_output.put_line('it is over');
vnum :=vnum+1;
end if;
vnum :=mycur%rowcount;
dbms_output.put_line(vnum||' owner is '||vowner||','||'vid is '||vid);
if mycur%notfound then
dbms_output.put_line('there is no data finded!');
end if;
exit when mycur%notfound;
end loop;
close mycur;
end;
輸出為:
owner is SYS,vid is 1
owner is SYS,vid is 46
owner is SYS,vid is 28
owner is SYS,vid is 15
owner is SYS,vid is 29
owner is SYS,vid is 3
it is over
owner is SYS,vid is 3
there is no data finded!
======================================================================
此例子與上一例子的區別是 exit when 語句的位置。放在了fetch 之後。
結果集被取出完畢之後,便退出loop。下面的語句不再執行。
declare
vid t.object_id%type;
vowner t.owner%type;
vnum integer;
cursor mycur is select object_id ,owner from t where rownum <7;
begin
-- select object_id ,owner into vid,vowner from t where rownum =2;
open mycur;
loop
fetch mycur into vid,vowner;
exit when mycur%notfound;
if vnum = 6 then
dbms_output.put_line('it is over');
vnum :=vnum+1;
end if;
vnum :=mycur%rowcount;
dbms_output.put_line(vnum||'owner is '||vowner||','||'vid is '||vid);
if mycur%notfound then
dbms_output.put_line('there is no data finded!');
end if;
end loop;
close mycur;
end;
輸出為:
1 owner is SYS,vid is 1
2 owner is SYS,vid is 46
3 owner is SYS,vid is 28
4 owner is SYS,vid is 15
5 owner is SYS,vid is 29
6 owner is SYS,vid is 3
第一個例子中迴圈在 it is over 提示後,又重複輸出了 owner is SYS,vid is 3;這是因為當提取到遊標結果集的最後一行之後,不再提取其他的行。所以變數 vid ,vowner 不會被改變,當再次輸出這些變數的語句時,就會重複輸出了
declare
vid t.object_id%type;
vowner t.owner%type;
vnum integer;
cursor mycur is select object_id ,owner from t where rownum <7;
begin
-- select object_id ,owner into vid,vowner from t where rownum =2;
open mycur;
loop
fetch mycur into vid,vowner;
if vnum = 6 then
dbms_output.put_line('it is over');
vnum :=vnum+1;
end if;
vnum :=mycur%rowcount;
dbms_output.put_line(vnum||' owner is '||vowner||','||'vid is '||vid);
if mycur%notfound then
dbms_output.put_line('there is no data finded!');
end if;
exit when mycur%notfound;
end loop;
close mycur;
end;
輸出為:
owner is SYS,vid is 1
owner is SYS,vid is 46
owner is SYS,vid is 28
owner is SYS,vid is 15
owner is SYS,vid is 29
owner is SYS,vid is 3
it is over
owner is SYS,vid is 3
there is no data finded!
======================================================================
此例子與上一例子的區別是 exit when 語句的位置。放在了fetch 之後。
結果集被取出完畢之後,便退出loop。下面的語句不再執行。
declare
vid t.object_id%type;
vowner t.owner%type;
vnum integer;
cursor mycur is select object_id ,owner from t where rownum <7;
begin
-- select object_id ,owner into vid,vowner from t where rownum =2;
open mycur;
loop
fetch mycur into vid,vowner;
exit when mycur%notfound;
if vnum = 6 then
dbms_output.put_line('it is over');
vnum :=vnum+1;
end if;
vnum :=mycur%rowcount;
dbms_output.put_line(vnum||'owner is '||vowner||','||'vid is '||vid);
if mycur%notfound then
dbms_output.put_line('there is no data finded!');
end if;
end loop;
close mycur;
end;
輸出為:
1 owner is SYS,vid is 1
2 owner is SYS,vid is 46
3 owner is SYS,vid is 28
4 owner is SYS,vid is 15
5 owner is SYS,vid is 29
6 owner is SYS,vid is 3
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-687930/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- pl/sql中三種遊標迴圈效率對比SQL
- for 迴圈境實現遊標LOOP提取OOP
- PL/SQL 迴圈SQL
- PL/SQL 遊標SQL
- Oracle PL/SQL迴圈示例OracleSQL
- PL/SQL 04 遊標 cursorSQL
- pl/sql for loop迴圈的使用SQLOOP
- PL/SQL迴圈控制語句SQL
- 使用遊標迴圈進行SQL更新插入的SQL語句SQL
- PL/SQL-遊標和遊標變數的使用SQL變數
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-FOR迴圈SQL
- oracle cursor遊標迴圈比較遊標元素是否相同Oracle
- Oracle顯示遊標的使用及遊標for迴圈Oracle
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-FOR迴圈-FOR迴圈中的索引SQL索引
- Oralce之PL/SQL程式設計(遊標)SQL程式設計
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-WHILE迴圈SQLWhile
- oracle pl/sql 迴圈比較集合元素是否相同OracleSQL
- 6.4. PL/SQL語法——6.4.6. 遊標SQL
- 【PL/SQL 學習】隱式遊標學習SQL
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-FOR迴圈-下限和上限SQL
- 6.4. PL/SQL語法——6.4.5. 迴圈語句SQL
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-基本迴圈(EXIT語句)SQL
- PL/SQL 中如何正確選擇遊標型別SQL型別
- 【MySql】MySql儲存,遊標,迴圈的簡單使用MySql
- MySQL遊標多迴圈一次的解決方法MySql
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-CONTINUESQL
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-EXIT WHENSQL
- SQL 遊標SQL
- 遊標和遞迴sql 的一些程式碼遞迴SQL
- SQL Server 2014如何使用遊標迴圈向遠端資料庫插入資料SQLServer資料庫
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-CONTINUE WHENSQL
- SQL Server遊標SQLServer
- SQL 遊標cursorSQL
- 關於pl/sql中流程控制和迴圈的一些總結SQL
- Oracle PL/SQL 關於遊標的介紹OracleSQL
- Sql Server系列:遊標SQLServer
- 一步步學MongoDB之使用迴圈來遍歷遊標MongoDB
- SQL儲存過程迴圈SQL儲存過程