Thread: function param and declared variable of same name

function param and declared variable of same name

From
Sim Zacks
Date:
<meta http-equiv=3D"content-type" content=3D"text/html; charset=3DISO-8=
859-1">body
      p { margin-bottom: 0cm; margin-top: 0pt; }

  <body style=3D"direction: ltr;"
    bidimailui-detected-decoding-type=3D"latin-charset" bgcolor=3D"#ffffff"
    text=3D"#000000">
    Tested in 8.2 and 9.0.1
    In plpgsql, if you have a function parameter and a variable of the
    same name, it ignores the value passed in and initializes the
    variable to null.
    The correct action, IMO, would be to raise an error.

    create or replace function test1(x int) returns int as
    $$
    declare x int;
    begin
    =A0=A0=A0 return x;
    end;
    $$ language 'plpgsql';

    Thanks
    Sim

Re: function param and declared variable of same name

From
Sim Zacks
Date:
On 09/01/2011 09:58 AM, Pavel Stehule wrote: <blockquote
cite="mid:CAFj8pRBtVU4Ry5VJL6CRBzEOqbOA7se38aLORY6ooPAnNa0eUg@mail.gmail.com"type="cite"><pre wrap="">2011/9/1 Sim
Zacks<a class="moz-txt-link-rfc2396E" href="mailto:sim@compulab.co.il"><sim@compulab.co.il></a>:
 
</pre><blockquote type="cite"><pre wrap="">Tested in 8.2 and 9.0.1

In plpgsql, if you have a function parameter and a variable of the same
name, it ignores the value passed in and initializes the variable to null.
The correct action, IMO, would be to raise an error.
</pre></blockquote><pre wrap="">
You can access params via $n conventions. functions params and
functions variables are not in same space, so it should not be a
error. What behave is in PL/SQL? There should be problem with backward
compatibility when we change it.

Regards

Pavel Stehule</pre></blockquote> While you _can_ access the variable using the $n convention, it is an obvious mistake
inthe function definition. It may break existing functions, but all of those functions have bugs in them that will then
becaught. Can you think of any valid case for passing in a variable and having the variable name refer to something
else?<br /><br /> Sim<br />