Oracle Drop Table If Exists Method

In Oracle, you can use the following statement to check if a table exists and drop the table:

DECLARE
   v_table_exists NUMBER;
BEGIN
   SELECT COUNT(*)
   INTO v_table_exists
   FROM user_tables
   WHERE table_name = 'YOUR_TABLE_NAME';
   
   IF v_table_exists > 0 THEN
      EXECUTE IMMEDIATE 'DROP TABLE YOUR_TABLE_NAME';
      DBMS_OUTPUT.PUT_LINE('Table YOUR_TABLE_NAME dropped successfully.');
   ELSE
      DBMS_OUTPUT.PUT_LINE('Table YOUR_TABLE_NAME does not exist.');
   END IF;
END;
/

Replace YOUR_TABLE_NAME with the name of the table you want to work with. This code will first check if the table exists, and if it does, will delete the table. If it does not exist, it will display a message.

bannerAds