Re: Create table if not exists ... how ??

Поиск
Список
Период
Сортировка
От Steve Atkins
Тема Re: Create table if not exists ... how ??
Дата
Msg-id FD3B66B0-64FD-43C3-9F6E-1A8A6868DC48@blighty.com
обсуждение исходный текст
Ответ на Re: Create table if not exists ... how ??  (Jennifer Trey <jennifer.trey@gmail.com>)
Список pgsql-general
On Jul 19, 2010, at 10:43 AM, Jennifer Trey wrote:

> No.... I don't want to drop it ... there is valuable data in there! I only want to create it if it doesn't already
exist...likely going to happen first time the application will run. I want to create the table then and populate. But
notthe next time. 
>
> Should I just let Java throw and exception and catch it ? Write a function for this would be optimal, although I have
noidea what the correct syntax is. 
>
> Cheers, Jen

Try something like this:

    create or replace function build_foo_table() returns void as $$
      create table foo (bar int);
    $$ language sql;

    select case when (select count(*) from information_schema.tables where table_name='foo')=0 then build_foo_table()
end;

    drop function build_foo_table();

Cheers,
  Steve


В списке pgsql-general по дате отправления:

Предыдущее
От: Vick Khera
Дата:
Сообщение: Re: Inheritance and trigger/FK propagation
Следующее
От: Said Ramirez
Дата:
Сообщение: Re: Create table if not exists ... how ??