Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop table from oracle database if table exist in sql statement [duplicate]

I have to use an SQL statement to drop a table, it will crash if the table doesn't exist. Is it possible to use IF statement to drop the table s.executeUpdate("DROP TABLE employee");

like image 698
user3500109 Avatar asked Dec 13 '25 23:12

user3500109


1 Answers

Oracle does not support a construct like drop table if exists my_table, which is apparently legal syntax in MySQL (and possibly other RDBMSs).

In a .SQL script, where you're running DDL to DROP and/or CREATE various objects, the Oracle standard is to drop the object, and ignore the error in cases where the object does not exist. If you wish, you can write code to check if the object exists (see DBA_OBJECTS view) to only drop if it exists.

from the s.executeUpdate, I gather that you're doing this in Java? If it was me, I'd just do the drop and ignore any not exists error.

like image 179
Mark J. Bobak Avatar answered Dec 15 '25 12:12

Mark J. Bobak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!