dbTalk Databases Forums  

DROP TABLE IF EXISTS

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss DROP TABLE IF EXISTS in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Sean Davis
 
Posts: n/a

Default DROP TABLE IF EXISTS - 10-21-2004 , 11:26 AM






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

Thanks,
Sean


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly


Reply With Quote
  #2  
Old   
Steven Klassen
 
Posts: n/a

Default Re: DROP TABLE IF EXISTS - 10-21-2004 , 11:37 AM






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

Quote:
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...5/msg00102.php

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

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
  #3  
Old   
William Yu
 
Posts: n/a

Default Re: DROP TABLE IF EXISTS - 10-25-2004 , 01:05 PM



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:

Quote:
* Sean Davis <sdavis2 (AT) mail (DOT) nih.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...5/msg00102.php


Reply With Quote
  #4  
Old   
Davis, Sean
 
Posts: n/a

Default Re: DROP TABLE IF EXISTS - 10-25-2004 , 03:28 PM



Point taken. However, in a perl/DBI setting, the program does die unless I
explicitly trap the error, etc. So, it is a convenience, true enough, but
not an issue that I had to deal with in MySQL, so just thought I would ask.

Sean


-----Original Message-----
From: William Yu
To: pgsql-novice (AT) postgresql (DOT) org
Sent: 10/25/2004 2:05 PM
Subject: Re: [NOVICE] DROP TABLE IF EXISTS

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:

Quote:
* Sean Davis <sdavis2 (AT) mail (DOT) nih.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...5/msg00102.php

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org



Reply With Quote
  #5  
Old   
William Yu
 
Posts: n/a

Default Re: DROP TABLE IF EXISTS - 10-25-2004 , 09:32 PM



That's a new one to me. I use perl/DBI also and my scripts just ignore
errors w/o any error trapping. Maybe my defaults are set to ignore
errors. I only trap errors when I there's a possibility of catastrophic
failure. (Example, DB is down -- SELECT * FROM zzz returns an error so
the row count is 0 -- page looks normal except there's no content versus
some generic apache error message.)




Davis, Sean (NIH/NHGRI) wrote:
Quote:
Point taken. However, in a perl/DBI setting, the program does die unless I
explicitly trap the error, etc. So, it is a convenience, true enough, but
not an issue that I had to deal with in MySQL, so just thought I would ask.

Reply With Quote
  #6  
Old   
Jaime Casanova
 
Posts: n/a

Default Re: DROP TABLE IF EXISTS - 10-26-2004 , 03:51 PM



--- William Yu <wyu (AT) talisys (DOT) com> escribió:
Quote:
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.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...5/msg00102.php


what about the Ron Johnson solution?
if exists (select 1 from pg_tables where tablename =
"thetable")
Quote:
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

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
  #7  
Old   
Sean Davis
 
Posts: n/a

Default Re: DROP TABLE IF EXISTS - 10-26-2004 , 04:35 PM



Jaime,

Thanks for the reply.

On Oct 26, 2004, at 4:51 PM, Jaime Casanova wrote:
Quote:
http://archives.postgresql.org/pgsql...5/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


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.