DB2 Drop Table question

When I drop a table that exists, the command responds successfully

db2 => drop table catalog
DB20000I  The SQL command completed successfully.

Open in new window


When I drop a table that does not exist, the command throws an error

db2 => drop table catalog
DB21034E  The command was processed as an SQL statement because it was not a
valid Command Line Processor command.  During SQL processing it returned:
SQL0204N  "DB2INST1.CATALOG" is an undefined name.  SQLSTATE=42704

Open in new window


The problem is that if I place a drop table inside of a script, and the table does not exist, then the script dies and all commands following the 'drop table' command are not executed

Is there any way to keep the drop table command from throwing an error if the table does not already exist ?
Los Angeles1Asked:
Who is Participating?
 
momi_sabagCommented:
short answer - no

long answer - you have two ways to face this problem:
1) create a stored procedure that will receive the table name as a parameter. If the table exists, it will drop it
The stored procedure will always finish successfully

2) Don't know which scripting language you use, but in your script you can try to run a query against syscat.table and see if the table already exists. If yes, drop it, else don't
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.