Re: Conditionally altering tables

Поиск
Список
Период
Сортировка
От Colin McGuigan
Тема Re: Conditionally altering tables
Дата
Msg-id 41E3EA87.2020208@earthcomber.com
обсуждение исходный текст
Ответ на Re: Conditionally altering tables  ("Mike G." <mike@thegodshalls.com>)
Список 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 по дате отправления:

Предыдущее
От: KÖPFERL Robert
Дата:
Сообщение: Major Problems with pg_dump
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Major Problems with pg_dump