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

文檔中的解釋: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;

執行上面的語句,結果為:

a

b

b

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

發現了另一個疑問:

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

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

再寫一段代碼

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;

執行代碼的結果:

hehe

疑問:游標是空游標,也就是說游標在打開的時候就沒有指向任何的值。但為什麼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有三種狀態,true,false,null。所以以後為了安全期間可以加上是否為空的判斷

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值