获取后 %NOTFOUND 可以返回 null 吗?

Can %NOTFOUND return null after a fetch?(获取后 %NOTFOUND 可以返回 null 吗?)
本文介绍了获取后 %NOTFOUND 可以返回 null 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题提出了一个非常有趣的观点;Oracle 文档中关于 %NOTFOUND 在获取后是否可能为空似乎存在矛盾.是吗?

This question raised a very interesting point; there seems to be a contradiction in the Oracle documentation on whether it's possible for %NOTFOUND to be null after a fetch. Is it?

引用 11g 文档

注意:在例 6-16 中,如果 FETCH 从不获取一行,则 c1%NOTFOUND始终为 NULL,并且永远不会退出循环.防止无限循环,请改用此 EXIT 语句:EXIT WHEN c1%NOTFOUND OR(c1%NOTFOUND 为空);

Note: In Example 6-16, if FETCH never fetches a row, then c1%NOTFOUND is always NULL and the loop is never exited. To prevent infinite looping, use this EXIT statement instead: EXIT WHEN c1%NOTFOUND OR (c1%NOTFOUND IS NULL);

文档似乎直接自相矛盾,因为它还说明了以下内容,这意味着在获取后 %NOTFOUND cannot 为 null.

The documentation seems to directly contradict itself as it also says the following, which implies that after a fetch %NOTFOUND cannot be null.

%NOTFOUND(%FOUND 的逻辑相反)返回:
在显式游标打开之后但在第一次获取之前为 NULL
如果最近从显式游标中获取的数据返回了一行,则为 FALSE
否则为真

%NOTFOUND (the logical opposite of %FOUND) returns:
NULL after the explicit cursor is opened but before the first fetch
FALSE if the most recent fetch from the explicit cursor returned a row
TRUE otherwise

10g 文档类似的警告,这不一定是直接矛盾,因为它警告可能无法成功执行获取以显示此行为.

The 10g documentation has a similar warning, which isn't, necessarily, a direct contradiction as it warns that a fetch might not execute successfully in order for this behaviour to be exhibited.

在第一次获取之前,%NOTFOUND 评估为 NULL.如果 FETCH 从不成功执行,EXIT WHEN 条件从不为 TRUE,并且循环永远不会退出.为了安全起见,您可能需要使用以下内容改为退出语句:

Before the first fetch, %NOTFOUND evaluates to NULL. If FETCH never executes successfully, the EXIT WHEN condition is never TRUE and the loop is never exited. To be safe, you might want to use the following EXIT statement instead:

当 c1%NOTFOUND 或 c1%NOTFOUND 为空时退出;

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

在什么情况下获取可能失败"或在执行获取后 %NOTFOUND 可能返回 null?

In what situations might a fetch either "fail" or might %NOTFOUND return null after a fetch has been executed?

推荐答案

我可以找到一个 fetch 可能失败的情况:

I can find a situation where a fetch can fail:

declare
  i integer;
  cursor c is
    select 1 / 0 from dual;
begin
  open c;

  begin
    fetch c
      into i;
  exception
    when others then
      dbms_output.put_line('ex');
  end;

  if c%notfound is null then
    dbms_output.put_line('null');
  elsif c%notfound then
    dbms_output.put_line('true');
  else
    dbms_output.put_line('false');
  end if;
  close c;

end;

但这只会让你的问题更强烈,因为它会评估为空,无论是在 10g 还是 11g ...

But this only makes your question stronger since it will evaluate to null, neither in 10g nor in 11g ...

这篇关于获取后 %NOTFOUND 可以返回 null 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

SQL to Generate Periodic Snapshots from Transactions Table(用于从事务表生成定期快照的SQL)
MyBatis support for multiple databases(MyBatis支持多个数据库)
Oracle 12c SQL: Missing column Headers in result(Oracle 12c SQL:结果中缺少列标题)
SQL query to find the number of customers who shopped for 3 consecutive days in month of January 2020(查询2020年1月连续购物3天的客户数量)
How to get top 10 data weekly (This week, Previous week, Last month, 2 months ago, 3 month ago)(如何每周获取前十大数据(本周、前一周、上个月、2个月前、3个月前))
Select the latest record for an Id per day - Oracle pl sql(选择每天ID的最新记录-Oracle pl SQL)