oracle数据库%notfound的理解
文档中的解释: 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
发现最后一条记录被打印了两次。原因是 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;
执行代码的结果: 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;
也就是说 oracle数据库中sql%notfound的用法详解
begin update table_name set salary = 10000 where emp_id = 10;
if sql%notfound then
insert into table_name (id,name,salary)values("","","") ;
end if;
end;
当
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |