DB2 Drop table, if equivalent

I need to discard the DB2 table if it exists, or discard and ignore errors.

+6
source share
3 answers

First query if table exists e.g.

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

and if he returns something, he will give out your

 drop table myschema.mytable 

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

+1
source

Try the following:

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

search on systable: if you are on as400 (power i, system i), the name of the system table is QSYS2.SYSTABLES still try sysibm.systables or syscat.tables (it 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 ; 
0
source

Source: https://habr.com/ru/post/902399/


All Articles