{"id":860,"date":"2009-05-07T18:26:46","date_gmt":"2009-05-08T01:26:46","guid":{"rendered":"http:\/\/journal2.alanv.org\/?p=860"},"modified":"2012-10-07T22:12:51","modified_gmt":"2012-10-08T02:12:51","slug":"plsql-fun","status":"publish","type":"post","link":"https:\/\/journal2.alanv.org\/?p=860","title":{"rendered":"plsql fun"},"content":{"rendered":"<p>Here&#8217;s a fun little question for those of you who aren&#8217;t experts at plsql&#8230;<\/p>\n<p>Given this plsql code<\/p>\n<pre>procedure helper_method(\r\n   iChange IN boolean,\r\n   oValue OUT number\r\n) IS\r\nBEGIN\r\n    IF iChange THEN\r\n        oValue := 2;\r\n    END IF;\r\nEND helper_method;\r\n\r\nprocedure do_test(\r\n    oValue OUT number\r\n) IS\r\nBEGIN\r\n    oValue := 1;\r\n    --Checkpoint 0\r\n    helper_method(true, oValue);\r\n    --Checkpoint 1\r\n    helper_method(false, oValue);\r\n    --Checkpoint 2\r\nEND do_test;<\/pre>\n<p>and this java code (assuming the right frameworks for calling into plsql)<\/p>\n<pre>public void doTest() {\r\n    IntOutParam out = new IntOutParam();\r\n    sqlCall(\"do_test\", out);\r\n    System.out.println(out.getInt());\r\n}<\/pre>\n<p>what is printed out when you replace the checkpoint comments with a <tt>RETURN;<\/tt> statement?<br \/>\nThe answer surprised me&#8230; I actually stumbled across this at work today and it took me a while to figure out.<\/p>\n<p>Replacing checkpoint 0 with <tt>RETURN;<\/tt> prints out 1, as expected.<br \/>\nReplacing checkpoint 1 with <tt>RETURN;<\/tt> prints out 2, as expected.<br \/>\nReplacing checkpoint 2 with <tt>RETURN;<\/tt> prints out 0. Huh?<\/p>\n<p>Can you spot why and what the fix should be?<br \/>\nHint: Swapping the order of the <tt>true<\/tt>\/<tt>false<\/tt> calls would result in outputs of 1, 0, and 2 respectively.<br \/>\nHuge Hint: At checkpoint 2, the value of <tt>oValue<\/tt> is actually <tt>null<\/tt>.<br \/>\nSee comments for the solution.<\/p>\n<p>So yeah, now you know something to watch out for when programming in plsql :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a fun little question for those of you who aren&#8217;t experts at plsql&#8230; Given this plsql code procedure helper_method( iChange IN boolean, oValue OUT number ) IS BEGIN IF iChange THEN oValue := 2; END IF; END helper_method; procedure &hellip; <a href=\"https:\/\/journal2.alanv.org\/?p=860\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-860","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/journal2.alanv.org\/index.php?rest_route=\/wp\/v2\/posts\/860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/journal2.alanv.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/journal2.alanv.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/journal2.alanv.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/journal2.alanv.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=860"}],"version-history":[{"count":0,"href":"https:\/\/journal2.alanv.org\/index.php?rest_route=\/wp\/v2\/posts\/860\/revisions"}],"wp:attachment":[{"href":"https:\/\/journal2.alanv.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/journal2.alanv.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/journal2.alanv.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}