Re: passing column name to a PL/pgsql function for ALTER TABLE ADD - Mailing list pgsql-novice

From Michael Fuhr
Subject Re: passing column name to a PL/pgsql function for ALTER TABLE ADD
Date
Msg-id 20050811142230.GA91641@winnie.fuhr.org
Whole thread Raw
In response to passing column name to a PL/pgsql function for ALTER TABLE ADD  (Alexander Kotelnikov <sacha@myxomop.com>)
List pgsql-novice
On Thu, Aug 11, 2005 at 02:50:11PM +0400, Alexander Kotelnikov wrote:
>
> Is it possible? I would like to do something like
> CREATE OR REPLACE FUNCTION add_column(name, anyelement) RETURNS integer AS '
>   DECLARE
>     col_name ALIAS FOR $1;
>     def_val  ALIAS FOR $2;
>   BEGIN
>     ALTER TABLE my_table ADD col_name def_val%TYPE;
>     RETURN 0;
>   END;
> ' LANGUAGE plpgsql;
> SELECT add_column('a', 1);

Do you really need this function to be polymorphic, or can you pass
the new column's type as a text argument?  In any case, you'll
probably want to read "Executing Dynamic Commands" in the PL/pgSQL
documentation:

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

--
Michael Fuhr

pgsql-novice by date:

Previous
From: Tom Lane
Date:
Subject: Re: connection trouble
Next
From: Tom Lane
Date:
Subject: Re: connection trouble