Re: Function - Mailing list pgsql-novice

From John DeSoi
Subject Re: Function
Date
Msg-id 44685007-C8C4-4621-8AD6-0E1ACBD6FDE2@pgedit.com
Whole thread Raw
In response to Function  (bill house <wchouse@bellsouth.net>)
Responses Re: Function  (bill house <wchouse@bellsouth.net>)
List pgsql-novice
On Feb 14, 2010, at 5:17 PM, bill house wrote:

> CREATE OR REPLACE FUNCTION disp_dup_recs(varchar, varchar)
>  RETURNS SETOF test_table
>  AS $$ SELECT *
>        FROM $1
>        WHERE $2
>        IN (SELECT $2
>            GROUP BY $2
>            HAVING ( COUNT($2) > 1 )
>           );
>      $$
>    LANGUAGE SQL
>    STABLE;
>
> --returns
> --ERROR:  syntax error at or near "$1"
> --LINE 81:         FROM $1


You can't build SQL statements like this. If you want to build a statement dynamically (where the table name and column
referencesare not known when the function is defined) you need to use EXECUTE. See 

http://www.postgresql.org/docs/8.4/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN


John DeSoi, Ph.D.





pgsql-novice by date:

Previous
From: Greg Stark
Date:
Subject: Re: Actions requiring commit
Next
From: "Lee Horowitz"
Date:
Subject: Re: Actions requiring commit