如果集合为空,FETCH INTO 不会引发异常,不是吗?

发布于 2024-10-15 14:35:49 字数 642 浏览 3 评论 0原文

这是我正在尝试调试的一些实际代码:

BEGIN
            OPEN bservice (coservice.prod_id);

            FETCH bservice
            INTO  v_billing_alias_id, v_billing_service_uom_id, v_summary_remarks;

            CLOSE bservice;

            v_service_found := 1;
        -- An empty fetch is expected for some services.
        EXCEPTION
            WHEN OTHERS THEN
                v_service_found := 0;
        END;

当参数化游标 bservice(prod_id) 为空时,它将 NULL 提取到三个变量中并且不会引发异常。

那么编写这段代码并期望它抛出异常的人都是错误的,对吗?该注释似乎暗示着空获取是预期的,然后它设置一个标志以供以后处理,但我认为这段代码也不可能用空集进行测试。

显然,它应该使用 bservice%NOTFOUND 或 bservice%FOUND 或类似的。

Here is some actual code I'm trying to debug:

BEGIN
            OPEN bservice (coservice.prod_id);

            FETCH bservice
            INTO  v_billing_alias_id, v_billing_service_uom_id, v_summary_remarks;

            CLOSE bservice;

            v_service_found := 1;
        -- An empty fetch is expected for some services.
        EXCEPTION
            WHEN OTHERS THEN
                v_service_found := 0;
        END;

When the parametrized cursor bservice(prod_id) is empty, it fetches NULL into the three variables and does not throw an exception.

So whoever wrote this code expecting it to throw an exception was wrong, right? The comment seems to imply that and empty fetch is expected and then it sets a flag for later handling, but I think this code cannot possibly have been tested with empty sets either.

Obviously, it should use bservice%NOTFOUND or bservice%FOUND or similar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清泪尽 2024-10-22 14:35:49

当参数化游标 bservice(prod_id) 为空时,它会将 NULL 提取到三个变量中,并且不会引发异常。

错误

当 t 为时,它不会获取任何内容,并且不会覆盖任何值。

declare

  cursor c(dt in date) is 
    select dummy from dual 
     where dt > sysdate;

  dummy_ dual.dummy%type;

begin

  open c(sysdate + 2);
  fetch c into dummy_;
  close c;
  dbms_output.put_line('1: ' || dummy_);

  open c(sysdate - 2);
  fetch c into dummy_;
  close c;
  dbms_output.put_line('2: ' || dummy_);

end;
/

那么

1: X
2: X

,编写这段代码并期望它抛出异常的人都是错误的,对吗?

显然,它应该使用 bservice%NOTFOUND 或 bservice%FOUND 或类似的。

When the parametrized cursor bservice(prod_id) is empty, it fetches NULL into the three variables and does not throw an exception.

Wrong

When t is empty, it fetches nothing, and does not overwrite any value.

declare

  cursor c(dt in date) is 
    select dummy from dual 
     where dt > sysdate;

  dummy_ dual.dummy%type;

begin

  open c(sysdate + 2);
  fetch c into dummy_;
  close c;
  dbms_output.put_line('1: ' || dummy_);

  open c(sysdate - 2);
  fetch c into dummy_;
  close c;
  dbms_output.put_line('2: ' || dummy_);

end;
/

prints

1: X
2: X

So whoever wrote this code expecting it to throw an exception was wrong, right? Yes

Obviously, it should use bservice%NOTFOUND or bservice%FOUND or similar. Yes

浅唱ヾ落雨殇 2024-10-22 14:35:49

如果您想知道游标是否返回任何结果,请使用 %FOUND 游标属性:

        OPEN bservice (coservice.prod_id);

        FETCH bservice
        INTO  v_billing_alias_id, v_billing_service_uom_id, v_summary_remarks;

        -- An empty fetch is expected for some services.
        IF (bservice%FOUND) THEN
          v_service_found := 1;
        ELSE
          v_service_found := 0;
        END IF

        CLOSE bservice;

来自 游标属性< /a>

%发现

  • 如果游标已声明但未打开,则返回 INVALID_CURSOR;或者如果光标已关闭。
  • 如果游标已打开,但 fetch 尚未执行,则返回 NULL
  • 如果执行成功获取,则返回 TRUE
  • 如果未返回任何行,则返回 FALSE。

If you want to know if the cursor returned any result, use the %FOUND cursor attribute:

        OPEN bservice (coservice.prod_id);

        FETCH bservice
        INTO  v_billing_alias_id, v_billing_service_uom_id, v_summary_remarks;

        -- An empty fetch is expected for some services.
        IF (bservice%FOUND) THEN
          v_service_found := 1;
        ELSE
          v_service_found := 0;
        END IF

        CLOSE bservice;

from cursor attributes

%FOUND

  • Returns INVALID_CURSOR if cursor is declared, but not open; or if cursor has been closed.
  • Returns NULL if cursor is open, but fetch has not been executed
  • Returns TRUE if a successful fetch has been executed
  • Returns FALSE if no row was returned.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文