DB2 Drop table if exists equivalent

2019-02-16 12:41发布

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

标签: sql db2 sql-drop
3条回答
欢心
2楼-- · 2019-02-16 12:56

Try this one:

IF EXISTS (SELECT name FROM sysibm.systables WHERE name = 'tab_name') THEN
DROP TABLE tab_name;END IF;
查看更多
ら.Afraid
3楼-- · 2019-02-16 13:04

First query if the table exists, like

select tabname from syscat.tables where tabschema='myschema' and tabname='mytable'

and if it returns something issue your

drop table myschema.mytable

Other possibility is to just issue the drop command and catch the Exception that will be raised if the table does not exist. Just put that code inside try {...} catch (Exception e) { // Ignore } block for that approach.

查看更多
疯言疯语
4楼-- · 2019-02-16 13:05

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  ; 
查看更多
登录 后发表回答