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
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;
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。所以以后为了安全期间可以加上是否为空的判断