Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DB2 Drop table if exists equivalent

Tags:

sql

sql-drop

db2

I need to drop a DB2 table if it exists, or drop and ignore errors.

like image 613
Roman Iuvshin Avatar asked Nov 25 '11 15:11

Roman Iuvshin


People also ask

Why use DROP TABLE if exists?

The DROP TABLE statement deletes the specified table, and any data associated with it, from the database. The IF EXISTS clause allows the statement to succeed even if the specified tables does not exist. If the table does not exist and you do not include the IF EXISTS clause, the statement will return an error.

Can we use drop with where clause?

WHERE clause mainly used along with the DELETE command. No clause required along with DROP command. Actions performed by DELETE can be rolled back as it uses buffer. Actions performed by DROP can't be rolled back because it directly works on actual data.


2 Answers

Try this one:

IF EXISTS (SELECT name FROM sysibm.systables WHERE name = 'tab_name') THEN
DROP TABLE tab_name;END IF;
like image 78
deltascience Avatar answered Sep 20 '22 08:09

deltascience


search on systable : if you are on as400 (power i, system i) the system table name is QSYS2.SYSTABLES else try sysibm.systables or syscat.tables (This depends on the operating system)

BEGIN    
IF EXISTS (SELECT NAME FROM QSYS2.SYSTABLES WHERE TABLE_SCHEMA = 'YOURLIBINUPPER' AND TABLE_NAME = 'YOUTABLENAMEINUPPER') THEN           
  DROP TABLE YOURLIBINUPPER.YOUTABLENAMEINUPPER;                             
END IF;                                                        
END  ; 
like image 33
Esperento57 Avatar answered Sep 21 '22 08:09

Esperento57