Re: passing column name to a PL/pgsql function for ALTER TABLE ADD

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: passing column name to a PL/pgsql function for ALTER TABLE ADD
Date: 2005-08-11 14:22:30
Message-ID: 20050811142230.GA91641@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Vanessa Conchodon 2005-08-11 14:30:10 Re: connection trouble
Previous Message Tom Lane 2005-08-11 14:14:41 Re: connection trouble