This odd little piece of code was featured in the weekly PL/SQL Challenge quiz 12 March - 18 March 2016.
What do you think will be displayed after executing the following block?
After all, my_flag is initialized to NULL when declared, and I don't change the value.
But, lo and behold, you will see:
So what's going on? Well, we have a very confused and confusing piece of code: I have written a simple CASE (which is of the form CASE expression WHEN ...), but then my WHEN clauses follow a typical searched CASE format (CASE WHEN expr1 ... WHEN expr2 ...).
CASE is a really wonderful feature in PL/SQL (and many other languages, of course), but you need to make sure you use it properly.
What do you think will be displayed after executing the following block?
DECLARE my_flag BOOLEAN; BEGIN CASE my_flag WHEN my_flag IS NULL THEN DBMS_OUTPUT.PUT_LINE ('my_flag is NULL'); WHEN TRUE THEN DBMS_OUTPUT.PUT_LINE ('my_flag is TRUE'); ELSE DBMS_OUTPUT.PUT_LINE ('my_flag is FALSE'); END CASE; END; /At first glance (if you are like me), you would say "my_flag is NULL", right?
After all, my_flag is initialized to NULL when declared, and I don't change the value.
But, lo and behold, you will see:
my_flag is FALSECurious, right?
So what's going on? Well, we have a very confused and confusing piece of code: I have written a simple CASE (which is of the form CASE expression WHEN ...), but then my WHEN clauses follow a typical searched CASE format (CASE WHEN expr1 ... WHEN expr2 ...).
CASE is a really wonderful feature in PL/SQL (and many other languages, of course), but you need to make sure you use it properly.
At a first glance I thought it was something to do with boolean variable. Later, I realized it is the same for all datatypes. So, it actually happens whenever the element in the switch statement is null. Any comparison with null will not be evaluated hence it wall fall to catch all(else) condition. A good food for brain!! :)
ReplyDeleteThanks!!!
This comment has been removed by the author.
ReplyDeleteMisha, thanks for the added explanation. I should have done that myself, but I knew you would want to contribute. :-)
ReplyDeleteYou are absolutely right. The reason this "works" - no compile errors - is that I am "searching" for a Boolean value. Change the datatype of my_flag to, say, integer, and it's a no-go situation.
Gee, I was going to insert an image here to show everyone, but I guess I can't do that. Dumb Blogger. :-(
I will post it on my twitter feed - twitter.com/sfonplsql
And even with INTEGER you can get into trouble (if you really try) :-)
ReplyDeleteSomething like this would still compile:
DECLARE
my_flag integer;
BEGIN
CASE my_flag = 1 -- now it is BOOLEAN :-)
WHEN my_flag IS NULL
THEN
DBMS_OUTPUT.PUT_LINE ('my_flag is NULL');
WHEN TRUE
THEN
DBMS_OUTPUT.PUT_LINE ('my_flag is TRUE');
ELSE
DBMS_OUTPUT.PUT_LINE ('my_flag is FALSE');
END CASE;
END;
P.s. Lost my original post somehow...
Not sure what happened to your earlier comment, Misha. Maybe Blogger has a bad where clause for its delete. Or my likely I clicked a link I shouldn't. Here, for the benefit of my wonderful readers, is what Misha wrote (unfortunately out of order now):
ReplyDeleteAnd even more fun starts when you try set default values :-). For example, if you initialize my_flag with FALSE, the output will be "my_flag is NULL".
But the story becomes pretty straightforward if you take into an account the following:
- simple CASE statement doesn't evaluate against NULL as value. I.e. the following will always go to ELSE:
case your_variable
when NULL ....
else ...
end case
- all conditions are really resulting in BOOLEAN value. That's why a search case format is tolerated and doesn't cause any syntax issues. Oracle would first validate the condition from your example and build a real simple CASE:
CASE my_flag
WHEN TRUE --- my_flag IS NULL
THEN ...
WHEN TRUE
THEN ...
ELSE ...
END CASE;
Now Oracle checks that the variable is NULL and falls into ELSE clause - as expected.
I hope, it clarifies the issue (a bit :-) ).