Thread: BUG #1875: Function parameter names clash with table column names

BUG #1875: Function parameter names clash with table column names

From
"Byron"
Date:
The following bug has been logged online:

Bug reference:      1875
Logged by:          Byron
Email address:      byron.hammond@westnet.com.au
PostgreSQL version: 8.0.3
Operating system:   Windows XP
Description:        Function parameter names clash with table column names
Details:

-----------------------------
Error Details from PgAdminIII
-----------------------------
ERROR:  syntax error at or near "$1" at character 28
QUERY:  INSERT INTO test_table_0 ( $1 ,  $2 ,  $3 ) VALUES ( $4 ,  $5 ,  $6
)
CONTEXT:  PL/pgSQL function "test_function_0" line 2 at SQL statement

------------
Problem Code
------------
CREATE TABLE test_table_0 (
    a    varchar(255),
    b    varchar(255),
    c    varchar(255)
);

CREATE OR REPLACE FUNCTION test_function_0(a varchar, b varchar, c varchar)
RETURNS INTEGER AS $$
BEGIN
    INSERT INTO test_table_0 (a, b, c) VALUES (a, b, c);
    RETURN 1;
END
$$ LANGUAGE 'plpgsql';

SELECT test_function_0('a', 'b', 'c');

DROP FUNCTION test_function_0(varchar, varchar, varchar);
DROP TABLE test_table_0;

-------------------
Additional Comments
-------------------
It appears in this case that the parameter names of the
function cannot be the same as the column names used
by the INSERT statement -- clashes.

Re: BUG #1875: Function parameter names clash with table column names

From
David Fetter
Date:
On Sat, Sep 10, 2005 at 05:46:44PM +0100, Byron wrote:
>
> The following bug has been logged online:
>
> Bug reference:      1875
> Logged by:          Byron
> Email address:      byron.hammond@westnet.com.au
> PostgreSQL version: 8.0.3
> Operating system:   Windows XP
> Description:        Function parameter names clash with table column names
> Details:
>
> -----------------------------
> Error Details from PgAdminIII
> -----------------------------
> ERROR:  syntax error at or near "$1" at character 28
> QUERY:  INSERT INTO test_table_0 ( $1 ,  $2 ,  $3 ) VALUES ( $4 ,  $5 ,  $6
> )
> CONTEXT:  PL/pgSQL function "test_function_0" line 2 at SQL statement
>
> ------------
> Problem Code
> ------------
> CREATE TABLE test_table_0 (
>     a    varchar(255),
>     b    varchar(255),
>     c    varchar(255)
> );
>
> CREATE OR REPLACE FUNCTION test_function_0(a varchar, b varchar, c varchar)
> RETURNS INTEGER AS $$
> BEGIN
>     INSERT INTO test_table_0 (a, b, c) VALUES (a, b, c);
>     RETURN 1;
> END
> $$ LANGUAGE 'plpgsql';

This should read something like

CREATE OR REPLACE FUNCTION test_function_0(in_a varchar, in_b varchar, in_c varchar)
RETURNS INTEGER AS $$
BEGIN
    INSERT INTO test_table_0 (a, b, c) VALUES (in_a, in_b, in_c);
    IF FOUND THEN
        RETURN 1;
    ELSE
        RETURN 0;
    END IF;
END
$$ LANGUAGE 'plpgsql';

>
> SELECT test_function_0('a', 'b', 'c');
>
> DROP FUNCTION test_function_0(varchar, varchar, varchar);
> DROP TABLE test_table_0;
>
> -------------------
> Additional Comments
> -------------------
> It appears in this case that the parameter names of the function
> cannot be the same as the column names used by the INSERT statement
> -- clashes.

This is not really a bug.  If the things really are different, in this
case function input parameters and column names, they should also have
different names.  PL/PgSQL merely enforces this :)

Cheers,
D
--
David Fetter david@fetter.org http://fetter.org/
phone: +1 510 893 6100   mobile: +1 415 235 3778

Remember to vote!