tag:blogger.com,1999:blog-7849367040589270673.post4861761836178859755..comments2024-03-21T22:50:39.997-07:00Comments on Obsessed with Oracle PL/SQL: The Case of the Confusing CASESteven Feuersteinhttp://www.blogger.com/profile/18405765731886460622noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-7849367040589270673.post-69655487327274484842016-03-23T08:05:02.753-07:002016-03-23T08:05:02.753-07:00Not sure what happened to your earlier comment, Mi...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):<br /><br />And 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".<br /><br />But the story becomes pretty straightforward if you take into an account the following:<br />- simple CASE statement doesn't evaluate against NULL as value. I.e. the following will always go to ELSE:<br />case your_variable<br />when NULL ....<br />else ...<br />end case<br /><br />- 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:<br />CASE my_flag<br />WHEN TRUE --- my_flag IS NULL<br />THEN ...<br />WHEN TRUE<br />THEN ...<br />ELSE ...<br />END CASE;<br />Now Oracle checks that the variable is NULL and falls into ELSE clause - as expected.<br /><br />I hope, it clarifies the issue (a bit :-) ). Steven Feuersteinhttps://www.blogger.com/profile/18405765731886460622noreply@blogger.comtag:blogger.com,1999:blog-7849367040589270673.post-378869799496560842016-03-23T07:51:29.132-07:002016-03-23T07:51:29.132-07:00And even with INTEGER you can get into trouble (if...And even with INTEGER you can get into trouble (if you really try) :-)<br />Something like this would still compile:<br /><br /><br />DECLARE<br /> my_flag integer;<br />BEGIN<br /> CASE my_flag = 1 -- now it is BOOLEAN :-)<br /> WHEN my_flag IS NULL<br /> THEN<br /> DBMS_OUTPUT.PUT_LINE ('my_flag is NULL');<br /> WHEN TRUE<br /> THEN<br /> DBMS_OUTPUT.PUT_LINE ('my_flag is TRUE');<br /> ELSE<br /> DBMS_OUTPUT.PUT_LINE ('my_flag is FALSE');<br /> END CASE;<br />END;<br /><br />P.s. Lost my original post somehow...Michael "Misha" Rosenblumhttps://www.blogger.com/profile/05568300284769247997noreply@blogger.comtag:blogger.com,1999:blog-7849367040589270673.post-68026556263301434032016-03-23T07:23:25.934-07:002016-03-23T07:23:25.934-07:00Misha, thanks for the added explanation. I should ...Misha, thanks for the added explanation. I should have done that myself, but I knew you would want to contribute. :-)<br /><br />You 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.<br /><br />Gee, I was going to insert an image here to show everyone, but I guess I can't do that. Dumb Blogger. :-(<br /><br />I will post it on my twitter feed - twitter.com/sfonplsqlSteven Feuersteinhttps://www.blogger.com/profile/18405765731886460622noreply@blogger.comtag:blogger.com,1999:blog-7849367040589270673.post-34646873791580943302016-03-23T07:18:45.651-07:002016-03-23T07:18:45.651-07:00This comment has been removed by the author.Michael "Misha" Rosenblumhttps://www.blogger.com/profile/05568300284769247997noreply@blogger.comtag:blogger.com,1999:blog-7849367040589270673.post-28248650260737905382016-03-23T05:55:47.310-07:002016-03-23T05:55:47.310-07:00At a first glance I thought it was something to do...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!! :)<br />Thanks!!!Ganesh Sridhar Koteshwar (Manoj)https://www.blogger.com/profile/04910199999057522117noreply@blogger.com