Re: DROP TABLE IF EXISTS

Поиск
Список
Период
Сортировка
От Sean Davis
Тема Re: DROP TABLE IF EXISTS
Дата
Msg-id 04A38A8D-2797-11D9-8C43-000A95D7BA10@mail.nih.gov
обсуждение исходный текст
Ответ на Re: DROP TABLE IF EXISTS  (Jaime Casanova <systemguards@yahoo.com>)
Список pgsql-novice
Jaime,

Thanks for the reply.

On Oct 26, 2004, at 4:51 PM, Jaime Casanova wrote:
> http://archives.postgresql.org/pgsql-interfaces/2002-05/msg00102.php
>
>
> what about the Ron Johnson solution?
>  if exists (select 1 from pg_tables where tablename =
> "thetable")
>> drop table thetable
>>

Actually, Ron gave some other possibilities, but the query above does
NOT work (and was the original source of the question).  Just for
information, here is a function that I had come up with that works.  It
returns 1 or 0 just as a sanity check.

create or replace function drop_if_exists (text) returns INTEGER AS '
DECLARE
    tbl_name ALIAS FOR $1;
BEGIN
    IF (select count(*) from pg_tables where tablename=$1) THEN        EXECUTE
''DROP TABLE '' || $1;
        RETURN 1;
    END IF;
    RETURN 0;
END;
'
language 'plpgsql';

Sean


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

Предыдущее
От: Jaime Casanova
Дата:
Сообщение: Re: DROP TABLE IF EXISTS
Следующее
От: ishaame@bluewin.ch
Дата:
Сообщение: Cannot Create tables - on Windows XP