If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Drop Table If Exists

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Aug 2003
Location: AUS
Posts: 26
Drop Table If Exists

Is there any db2 command equivalent to mysql's DROP TABLE IF EXISTS

Cheers
Reply With Quote
  #2 (permalink)  
Old
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,713
No, that syntax is not supported. However, you can just drop the table and intercept the return code that DB2 returns if the table does not exist.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Aug 2003
Location: AUS
Posts: 26
I found a solution but I am not sure it's safe to do or not.

IF EXISTS (SELECT name FROM sysibm.systables WHERE name = 'tab_name') THEN
DROP TABLE tab_name;
END IF;

cheers
Melb
Reply With Quote
  #4 (permalink)  
Old
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,713
Keep in mind that you also need the TABSCHEMA column to uniquely qualify a row in the SYSCAT.TABLES. Also, not all entries in SYSCAT.TABLES are base tables (some are views, nicknames, etc), so use the TYPE column if that matters to you.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Jan 2004
Location: Tallahassee, FL, USA
Posts: 96
use case .. when statement , to drop table , if exists.


sample as below



select case when exist ( select 'X' from sysibm.systables where name 'PSLOCK' ) then ' drop table '||name else ' ' end from sysibm.systables where name = 'PSLOCK' and type = 'T'


Thank You


Lekharaju Ennam
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On