{"id":77,"date":"2007-03-13T17:25:25","date_gmt":"2007-03-13T16:25:25","guid":{"rendered":"http:\/\/bar-solutions.com\/wordpress\/?p=77"},"modified":"2007-03-13T17:25:30","modified_gmt":"2007-03-13T16:25:30","slug":"parameters-in-oracle","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=77","title":{"rendered":"Parameters in Oracle"},"content":{"rendered":"<p align=\"left\">Until today I was under the impression that parameters in PL\/SQL were unrestricted in size. That is only partially correct. I was playing around with a simple script to swap two variables without introducing a temporary variable (I know, completely useless, but fun to think about a bit).<\/p>\n<p align=\"left\">I created the following package<\/p>\n<blockquote>\n<p><font size=\"1\">CREATE OR REPLACE PACKAGE swap_test<br \/>IS<br \/>&nbsp; PROCEDURE swap(<br \/>&nbsp;&nbsp;&nbsp; number_a IN OUT NUMBER<br \/>&nbsp; , number_b IN OUT NUMBER ); <\/font><\/p>\n<p><font size=\"1\">&nbsp; PROCEDURE swap(<br \/>&nbsp;&nbsp;&nbsp; char_a IN OUT VARCHAR2<br \/>&nbsp; , char_b IN OUT VARCHAR2 );<br \/>END swap_test;<br \/>\/<br \/>CREATE OR REPLACE PACKAGE BODY swap_test<br \/>IS<br \/>&nbsp; PROCEDURE swap(<br \/>&nbsp;&nbsp;&nbsp; number_a IN OUT NUMBER<br \/>&nbsp; , number_b IN OUT NUMBER )<br \/>&nbsp; IS<br \/>&nbsp; BEGIN<br \/>&nbsp;&nbsp;&nbsp; number_a := number_a + number_b;<br \/>&nbsp;&nbsp;&nbsp; number_b := number_a &#8211; number_b;<br \/>&nbsp;&nbsp;&nbsp; number_a := number_a &#8211; number_b;<br \/>&nbsp; END swap; <\/font><\/p>\n<p><font size=\"1\">&nbsp; PROCEDURE swap(<br \/>&nbsp;&nbsp;&nbsp; char_a IN OUT VARCHAR2<br \/>&nbsp; , char_b IN OUT VARCHAR2 )<br \/>&nbsp; IS<br \/>&nbsp; BEGIN<br \/>&nbsp;&nbsp;&nbsp; char_a := char_a || char_b;<br \/>&nbsp;&nbsp;&nbsp; char_b := SUBSTR( char_a, 1, LENGTH( char_a ) &#8211; LENGTH( char_b ));<br \/>&nbsp; char_a := SUBSTR( char_a, LENGTH( char_b ) + 1, LENGTH( char_a ));<br \/>&nbsp; END swap;<br \/>END swap_test;<br \/>\/<\/font><\/p>\n<\/blockquote>\n<p align=\"left\">and then I tried the following script:<\/p>\n<blockquote>\n<p align=\"left\"><font size=\"1\">SET SERVEROUTPUT ON<br \/>DECLARE<br \/>&nbsp; a NUMBER;<br \/>&nbsp; b NUMBER;<br \/>&nbsp; c&nbsp;VARCHAR2( 10 );<br \/>&nbsp; d VARCHAR2( 10 );<br \/>BEGIN<br \/>&nbsp; a := 3;<br \/>&nbsp; b := 5;<br \/>&nbsp; DBMS_OUTPUT.put( &#8216; a=&#8217; || a );<br \/>&nbsp; DBMS_OUTPUT.put_line( &#8216; b=&#8217; || b );<br \/>&nbsp; swap_test.swap( a, b );<br \/>&nbsp; DBMS_OUTPUT.put( &#8216; a=&#8217; || a );<br \/>&nbsp; DBMS_OUTPUT.put_line( &#8216; b=&#8217; || b );<br \/>&nbsp; c := &#8216;Dana&#8217;;<br \/>&nbsp; d := &#8216;Patrick&#8217;;<br \/>&nbsp; DBMS_OUTPUT.put( &#8216; c=&#8217; || c );<br \/>&nbsp; DBMS_OUTPUT.put_line( &#8216; d=&#8217; || d );<br \/>&nbsp; swap_test.swap( c, d );<br \/>&nbsp; DBMS_OUTPUT.put( &#8216; c=&#8217; || c );<br \/>&nbsp; DBMS_OUTPUT.put_line( &#8216; d=&#8217; || d );<br \/>END;<br \/>\/<\/font><\/p>\n<\/blockquote>\n<p align=\"left\">And it gave me this error:<\/p>\n<blockquote>\n<p align=\"left\"><font size=\"1\">ORA-06502: PL\/SQL: numeric or value error<br \/>ORA-06512: at &#8220;SCOTT.SWAP_TEST&#8221;, line 18<br \/>ORA-06512: at line 19<\/font><\/p>\n<\/blockquote>\n<p align=\"left\">It appeared that the varchar2 variable was too small to hold the concatenated value of both. When I made the varchar2 variables a bit bigger (big enough to hold both values concatenated) the error went away.<\/p>\n<blockquote>\n<p align=\"left\"><font size=\"1\">SET SERVEROUTPUT ON<br \/>DECLARE<br \/>&nbsp;&nbsp;&#8230;.<br \/>&nbsp; c&nbsp;VARCHAR2( 20 );<br \/>&nbsp; d VARCHAR2( 20 );<br \/>BEGIN<br \/>&nbsp;&nbsp;&#8230;.<br \/><\/font><font size=\"1\">&nbsp; c := &#8216;Dana&#8217;;<br \/>&nbsp; d := &#8216;Patrick&#8217;;<br \/>&nbsp; DBMS_OUTPUT.put( &#8216; c=&#8217; || c );<br \/>&nbsp; DBMS_OUTPUT.put_line( &#8216; d=&#8217; || d );<br \/>&nbsp; swap_test.swap( c, d );<br \/>&nbsp; DBMS_OUTPUT.put( &#8216; c=&#8217; || c );<br \/>&nbsp; DBMS_OUTPUT.put_line( &#8216; d=&#8217; || d );<br \/>END;<br \/>\/<\/font><\/p>\n<\/blockquote>\n<p align=\"left\">But, what about the number then? Changed the script again from number into number(1) (just one position, so numbers 0 to 9). I also changed the values, so if they are added, they will be 10 or higher (taking up 2 positions)<\/p>\n<blockquote>\n<p align=\"left\"><font size=\"1\">SET SERVEROUTPUT ON<br \/>DECLARE<br \/>&nbsp; a NUMBER( 1 );<br \/>&nbsp; b NUMBER( 1 );<br \/>&nbsp; &#8230;.<br \/><\/font><font size=\"1\">BEGIN<br \/>&nbsp; a := 3;<br \/>&nbsp; b := 8;<br \/>&nbsp; DBMS_OUTPUT.put( &#8216; a=&#8217; || a );<br \/>&nbsp; DBMS_OUTPUT.put_line( &#8216; b=&#8217; || b );<br \/>&nbsp; swap_test.swap( a, b );<br \/>&nbsp; DBMS_OUTPUT.put( &#8216; a=&#8217; || a );<br \/>&nbsp; DBMS_OUTPUT.put_line( &#8216; b=&#8217; || b );<br \/>&nbsp; &#8230;.<br \/><\/font><font size=\"1\">END;<br \/>\/<\/font><\/p>\n<\/blockquote>\n<p align=\"left\">And hey, that worked flawlessly. So, apparently Oracle will change the dimensions of a number as needed, but it cannot change the dimensions of a varchar2 parameter at runtime. I think it has to do with the parameters being OUT parameters. I tried this on Oracle 8i and 10G XE and it gave me the same results. I don&#8217;t know if this should be considered a bug. I also tried to build the function using a subtype for the varchar2 parameters, using the biggest size possible, but I got the same error.<\/p>\n<p align=\"left\">Maybe someone can supply me with a solution for this (or more insight) except for the one I already supplied.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Until today I was under the impression that parameters in PL\/SQL were unrestricted in size. That is only partially correct. I was playing around with a simple script to swap two variables without introducing a temporary variable (I know, completely useless, but fun to think about a bit). I created the following package CREATE OR [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-oracle","category-plsql"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=77"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/77\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}