Re: Conditionally altering tables

From: Colin McGuigan <cmcguigan(at)earthcomber(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Conditionally altering tables
Date: 2005-01-11 15:02:31
Message-ID: 41E3EA87.2020208@earthcomber.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Mike G. wrote:
> Yes,
>
> Query pg_attribute table / catalog to see if it exists.
>
> See
http://www.postgresql.org/docs/7.4/interactive/catalog-pg-attribute.html
>
> HTH

I'm sorry; my question was more along the lines of, "How can I put this
into a SQL script and run it repeatedly without errors?" AFAIK,
Postgres doesn't support any sort of conditional outside of a procedure,
so something along the lines of:

IF NOT EXISTS (SELECT * FROM pg_class INNER JOIN pg_attribute ON
pg_class.oid = pg_attribute.attrelid WHERE pg_class.relname = 'MyTable'
AND pg_attribute.attname = 'NewField') ALTER TABLE MyTable ADD COLUMN
NewField integer NULL

Doesn't work -- syntax error near "IF". If I put it into a procedure
and run that, it'll work fine, but I'd prefer to avoid having to have a
different procedure for each schema change that may be made.

--Colin McGuigan

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2005-01-11 15:12:27 Re: Major Problems with pg_dump
Previous Message KÖPFERL Robert 2005-01-11 11:15:35 Major Problems with pg_dump