Re: DROP TABLE IF EXISTS

Lists: pgsql-novice
From: Sean Davis <sdavis2(at)mail(dot)nih(dot)gov>
To: Postgres <pgsql-novice(at)postgresql(dot)org>
Subject: DROP TABLE IF EXISTS
Date: 2004-10-21 16:26:47
Message-ID: 00FA48A1-237E-11D9-A489-000A95D7BA10@mail.nih.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

I am wondering how to do this simple mysql task in postgres. Any hints?

Thanks,
Sean


From: Steven Klassen <sklassen(at)commandprompt(dot)com>
To: Sean Davis <sdavis2(at)mail(dot)nih(dot)gov>
Cc: Postgres <pgsql-novice(at)postgresql(dot)org>
Subject: Re: DROP TABLE IF EXISTS
Date: 2004-10-21 16:37:00
Message-ID: 20041021163700.GB2946@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

* Sean Davis <sdavis2(at)mail(dot)nih(dot)gov> [2004-10-21 12:26:47 -0400]:

> I am wondering how to do this simple mysql task in postgres. Any
> hints?

This reply from Ron Johnson seems to suffice:

http://archives.postgresql.org/pgsql-interfaces/2002-05/msg00102.php

--
Steven Klassen - Lead Programmer
Command Prompt, Inc. - http://www.commandprompt.com/
PostgreSQL Replication & Support Services, (503) 667-4564


From: William Yu <wyu(at)talisys(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: DROP TABLE IF EXISTS
Date: 2004-10-25 18:05:36
Message-ID: cljf66$2prf$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

I'm not sure why this even matters. You do DROP TABLE on a table that
doesn't exist, all that will happen is that you will get an error back
but your program will continue on it's merry way anyways. The only thing
IF EXISTS would give you is the suppression of the error message.

Steven Klassen wrote:

> * Sean Davis <sdavis2(at)mail(dot)nih(dot)gov> [2004-10-21 12:26:47 -0400]:
>
>
>>I am wondering how to do this simple mysql task in postgres. Any
>>hints?
>
>
> This reply from Ron Johnson seems to suffice:
>
> http://archives.postgresql.org/pgsql-interfaces/2002-05/msg00102.php
>


From: Jaime Casanova <systemguards(at)yahoo(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: DROP TABLE IF EXISTS
Date: 2004-10-26 20:51:34
Message-ID: 20041026205134.34093.qmail@web50004.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

--- William Yu <wyu(at)talisys(dot)com> escribió:
> I'm not sure why this even matters. You do DROP
> TABLE on a table that
> doesn't exist, all that will happen is that you will
> get an error back
> but your program will continue on it's merry way
> anyways. The only thing
> IF EXISTS would give you is the suppression of the
> error message.
>
>
> Steven Klassen wrote:
>
> > * Sean Davis <sdavis2(at)mail(dot)nih(dot)gov> [2004-10-21
> 12:26:47 -0400]:
> >
> >
> >>I am wondering how to do this simple mysql task in
> postgres. Any
> >>hints?
> >
> >
> > This reply from Ron Johnson seems to suffice:
> >
> >
>
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

regards,
Jaime Casanova

_________________________________________________________
Do You Yahoo!?
Información de Estados Unidos y América Latina, en Yahoo! Noticias.
Visítanos en http://noticias.espanol.yahoo.com


From: Sean Davis <sdavis2(at)mail(dot)nih(dot)gov>
To: Jaime Casanova <systemguards(at)yahoo(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: DROP TABLE IF EXISTS
Date: 2004-10-26 21:35:55
Message-ID: 04A38A8D-2797-11D9-8C43-000A95D7BA10@mail.nih.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: 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