DB2 Drop table if exists equivalent

血红的双手。 提交于 2019-12-19 05:16:42

问题


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


回答1:


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.




回答2:


Try this one:

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



回答3:


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  ; 



回答4:


The below worked for me in DB2 which queries the SYSCAT.TABLES view to check if the table exists. If yes, it prepares and executes the DROP TABLE statement.

BEGIN    
   IF EXISTS (SELECT TABNAME FROM SYSCAT.TABLES WHERE TABSCHEMA = 'SCHEMA_NAME' AND TABNAME = 'TABLE_NAME') THEN 
      PREPARE stmt FROM 'DROP TABLE SCHEMA_NAME.TABLE_NAME';
      EXECUTE stmt;
   END IF;                                                        
END


来源:https://stackoverflow.com/questions/8270937/db2-drop-table-if-exists-equivalent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!