PL/SQL Brain Teaser: When is NO_DATA_FOUND not?

Here goes:

I execute this statement:

CREATE OR REPLACE FUNCTION ndf
   RETURN NUMBER
IS
BEGIN
   RAISE NO_DATA_FOUND;
END;
/

We all know what that function is going to do, right? #Fail, as one might say on Twitter.

So the brain teaser is: 

In the block below, replace <statement> with a single statement that does not contain any exception handling, but does call the NDF function, so that after the block executes, "NDF? What NDF?" is displayed on the screen.


DECLARE
   n   NUMBER;
BEGIN
   <statement>
   DBMS_OUTPUT.PUT_LINE ('NDF? What NDF?');
END;
/

I will wait a bit to post my answer. I encourage you to post yours as a comment to this blog post.

Wait....
Wait....
Wait for it....

OK!

After a couple of days of collecting responses on this post and also in LinkedIn, it's time to publish the answer:

You can replace <statement> with any DML statement that executes the function, and the failure of the function with an unhandled NO_DATA_FOUND exception will not cause the SQL statement to terminate with said exception.

Instead, the SQL engine swallows up that exception and simply returns NULL to the statement.

Why, you might wonder, would the SQL engine do this?

NO_DATA_FOUND is, on the one hand, an exception like any other. And on the other hand, it is different, in that the lack of data often does not indicate any kind of actual error, but simply a data condition. And so it was decided that when a function executed within a SQL statement fails with an unhandled NO_DATA_FOUND, that NULL would simply be returned.

You might not like that answer or decision, but there it is.

Now, there is another way to both invoke the NDF function in a single statement and not have the exception terminate the block, as Edwin points out in the comments: Call the function inside a COALESCE function call.

COALESCE offers the very cool feature of not evaluating an expression in its least until it needs to (in contrast, for example, to NVL, which always evaluates the second argument, even if the first argument is not NULL.

Update 12-21: Jeff Kemp (@jeffreykemp) notes on LinkedIn that since the text in the brain teaser says "call the NDF function", COALESCE is not a valid answer, since you never call the function. Strictly speaking he is correct.

Comments

  1. Hi Steven,

    How about this?

    DECLARE
    n NUMBER;
    BEGIN
    select ndf into n from dual;
    DBMS_OUTPUT.PUT_LINE ('NDF? What NDF?');
    END;
    /

    NDF? What NDF?

    PL/SQL procedure successfully completed.

    ReplyDelete
  2. Would you accept this :

    DECLARE
    n NUMBER;
    BEGIN
    select count(1) into n from dual where ndf() = 1;
    DBMS_OUTPUT.PUT_LINE ('NDF? What NDF?');
    END;
    /

    ReplyDelete
  3. At least this one

    n := coalesce(1, ndf);

    ReplyDelete
  4. and , perhaps surprisingly for some,

    select ndf into n from dual;

    (EE Release 12.1.0.2.0)

    ReplyDelete
  5. in ANSI SQL no_data_found is not an error ?

    ReplyDelete
  6. My first thought before reading the comments was to look for a way to short circuit the function call just like Edwin's first answer.

    It was indeed surprising for me, that the column list in a sql select-into doesn't raise NO_DATA_FOUND. I guess, the more you know.

    Any explanation on why it implicitly assumes a null instead of raising the error, or am I failing to notice something really obvious?

    I already spot checked other sql and plsql exceptions like INVALID_CURSOR, DUP_VAL_ON_INDEX, too_many_rows, etc. and it does raise all of them. (makes sense for too_many_rows)

    ReplyDelete
  7. DECLARE
    n NUMBER;
    BEGIN
    n := ndf();
    DBMS_OUTPUT.PUT_LINE ('NDF? What NDF?');
    END;
    /

    ReplyDelete
    Replies
    1. Sorry. The above code will throw an exception.
      The code that could possibly work is:
      DECLARE
      n NUMBER;
      BEGIN
      select nvl(ndf(),1) into n from dual;
      DBMS_OUTPUT.PUT_LINE ('NDF? What NDF?');
      END;

      Delete
  8. Oracle documentation says:

    NO_DATA_FOUND 01403 +100

    Because this exception is used internally by some SQL functions to signal completion, you must not rely on this exception being propagated if you raise it within a function that is invoked as part of a query.

    ReplyDelete
    Replies
    1. Exactly. NO_DATA_FOUND is, on the one hand, an exception like any other. And on the other hand, it is different, in that the lack of data often does not indicate any kind of actual error, but simply a data condition. And it was decided that when a function executed within a SQL statement fails with an unhandled NO_DATA_FOUND, that NULL would simply be returned.

      Delete
  9. Thanks, everyone! Excellent responses - especially that reminder about Coalesce. Edwin, I hadn't even thought of that!

    ReplyDelete

Post a Comment

Popular posts from this blog

Table Functions, Part 1: Introduction and Exploration

Recommendations for unit testing PL/SQL programs

The future of Oracle PL/SQL: some thoughts on Sten Vesterli's thoughts