Re: DROP TABLE IF EXISTS - Mailing list pgsql-novice

From Sean Davis
Subject Re: DROP TABLE IF EXISTS
Date
Msg-id 04A38A8D-2797-11D9-8C43-000A95D7BA10@mail.nih.gov
Whole thread Raw
In response to Re: DROP TABLE IF EXISTS  (Jaime Casanova <systemguards@yahoo.com>)
List 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 by date:

Previous
From: Jaime Casanova
Date:
Subject: Re: DROP TABLE IF EXISTS
Next
From: ishaame@bluewin.ch
Date:
Subject: Cannot Create tables - on Windows XP