Обсуждение: drop table if exists mytable;

Поиск
Список
Период
Сортировка

drop table if exists mytable;

От
"Anton Melser"
Дата:
Hi,
I need to do a "drop table if exists" type thing. I realise I can
easily look in pg_tables, but for testing (if), don't I need to use a
procedural language? In which case, I will need to install it if it
doesn't exist - but I don't know how to test to see whether a language
exists without using a language!
Am I missing something simple here? Is there no way to do this outside
of a function?
Cheers
Antoine

Re: drop table if exists mytable;

От
"Gurjeet Singh"
Дата:
I am not sure if I understood the problem correctly!!!

Can you not use the standard command "DROP TABLE IF EXISTS table1" that PG provides?

http://www.postgresql.org/docs/8.2/interactive/sql-droptable.html

Or is it that you are on a version of PG where "IF EXISTS" syntax is not available?

Regards,

--
gurjeet[.singh]@EnterpriseDB.com
singh.gurjeet@{ gmail | hotmail | yahoo }.com

On 2/10/07, Anton Melser <melser.anton@gmail.com> wrote:
Hi,
I need to do a "drop table if exists" type thing. I realise I can
easily look in pg_tables, but for testing (if), don't I need to use a
procedural language? In which case, I will need to install it if it
doesn't exist - but I don't know how to test to see whether a language
exists without using a language!
Am I missing something simple here? Is there no way to do this outside
of a function?
Cheers
Antoine

Re: drop table if exists mytable;

От
Andreas Kretschmer
Дата:
Anton Melser <melser.anton@gmail.com> schrieb:

> Hi,
> I need to do a "drop table if exists" type thing. I realise I can

Install 8.2 or use this function, posted by David Fetter:

--
-- posted by David Fetter
--
CREATE OR REPLACE FUNCTION drop_table(TEXT)
RETURNS VOID
STRICT
LANGUAGE plpgsql
AS $$
BEGIN
    BEGIN
    EXECUTE 'DROP TABLE ' || $1;
    EXCEPTION WHEN UNDEFINED_TABLE THEN
        /* do nothing */
    RETURN;
    END;
RETURN;
END;
$$;


Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."    (unknow)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°

Re: drop table if exists mytable;

От
"Anton Melser"
Дата:
> > I need to do a "drop table if exists" type thing. I realise I can
>
> Install 8.2 or use this function, posted by David Fetter:

Thanks for your answers... so this really was something that was
missing (I think it a little rich to come out with a "are you using a
version without this" when it has just come out!) ?
I am trying to add postgres support for alchemi (.net grid
infrastructure), and I wanted to change as little as possible. Seeing
as it didn't support postgres before, I suppose having 8.2 as a prereq
ain't to bad.
Cheers
Antoine