Re: Conditionally altering tables - Mailing list pgsql-novice

From Colin McGuigan
Subject Re: Conditionally altering tables
Date
Msg-id 41E3EA87.2020208@earthcomber.com
Whole thread Raw
In response to Re: Conditionally altering tables  ("Mike G." <mike@thegodshalls.com>)
List 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

pgsql-novice by date:

Previous
From: KÖPFERL Robert
Date:
Subject: Major Problems with pg_dump
Next
From: Tom Lane
Date:
Subject: Re: Major Problems with pg_dump