日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

oracle cur notfound,%notfound的理解——oracle存儲過程 | 學步園

發(fā)布時間:2025/3/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle cur notfound,%notfound的理解——oracle存儲過程 | 學步園 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文檔中的解釋:It returns TRUE if an INSERT, UPDATE, or DELETE statement affected no rows, or a SELECT INTO statement returned no rows. Otherwise, it returns FALSE.

這個解釋更加精妙:

%NOTFOUND is the logical opposite of %FOUND. %NOTFOUND yields FALSE if the last fetch returned a row, or TRUE if the last fetch failed to return a row

錯誤的例子:

tableA

id? name

1?? a

2?? b

declare

cursor v_cur is select name from tableA;

n varchar2(10);

begin

open v_cur;

loop

exit when v_cur%notfound;

fetch v_cur into n;

dbms_output.put_line(n);

close v_cur;

end loop;

end;

執(zhí)行上面的語句,結(jié)果為:

a

b

b

發(fā)現(xiàn)最後一條記錄被列印了兩次。原因是%notfound是判斷最後一次fetch的結(jié)果,把bfetch到變數(shù)n中之後再執(zhí)行exit when %notfound判斷得到的是false的記過,也就是說是有返回行的,所以判斷通過,再此執(zhí)行了列印語句。

發(fā)現(xiàn)了另一個疑問:

把a,b都fetch之後按理說游標(biāo)已經(jīng)空了,那麼第三次應(yīng)該是fetch的空值,為什麼列印出來的還是b呢??

因為fetch..into語句末尾不會修改into變數(shù)後面的值。就像select..into如果沒有數(shù)據(jù)會報異常,但是不會把into後面的變數(shù)置為空

再寫一段代碼

declare

cursor v_cur is select name from tableA where name = 'c';

n varchar2(10);

begin

open v_cur;

loop

exit when v_cur%notfound;

n:='hehe'

fetch v_cur into n;

dbms_output.put_line(n);

close v_cur;

end loop;

end;

執(zhí)行代碼的結(jié)果:

hehe

疑問:游標(biāo)是空游標(biāo),也就是說游標(biāo)在打開的時候就沒有指向任何的值。但為什麼exit when v_cur%notfound;這條語句還通過了呢??

oracle文檔的解釋:

Before the first fetch, %NOTFOUND returns NULL. If FETCH never executes successfully, the loop is never exited, because the EXIT WHEN statement executes only if its WHEN condition is true. To be safe, you might want to use the following EXIT statement instead:

EXIT WHEN c1%NOTFOUND OR c1%NOTFOUND IS NULL;

也就是說v_cur%notfound有三種狀態(tài),true,false,null。所以以後為了安全期間可以加上是否為空的判斷

總結(jié)

以上是生活随笔為你收集整理的oracle cur notfound,%notfound的理解——oracle存儲過程 | 學步園的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。