ORACLE: drop a table if it already exist

person Jason Huangfolder_openCode, Oraclelocal_offer, , , access_time May 5, 2015

Oracle does not have a simple if table exists, drop it command like in MySQL.

However, you can use this piece of code in your Oracle pSQL code to achieve the same thing:
catching the exception ORA-942 table not exist.


-- drop tables if exists TEST_TABLE
BEGIN
   EXECUTE IMMEDIATE 'DROP TABLE TEST_TABLE CASCADE CONSTRAINTS';
EXCEPTION
   WHEN OTHERS THEN
      IF SQLCODE != -942 THEN
         RAISE;
      END IF;
END;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>