drop Oracle table

Oracle knows the command “drop table”. One may include this in a script where a table is first deleted (using “drop table”) and subsequently rebuilt with additional SQL statement.
But what happens if the table doesn’t exist? One may then end up with an error SQL Error: ORA-00942: “table or view does not exist”.

How to avoid this error?
Oracle doesn’t have a clause that reads as “if exists then drop table”. The solution to avoid this error is to set up a procedure that reads like:

create or replace PROCEDURE DROP_TABLE
(
  P_TABLE_NAME IN VARCHAR2
) AS
n_table           INT;
stmt              VARCHAR(100);
BEGIN
  stmt := 'drop table ' || P_TABLE_NAME;
  SELECT COUNT(*) INTO n_table FROM user_tables WHERE table_name = UPPER(p_table_name);
  IF (n_table > 0) THEN
    EXECUTE IMMEDIATE stmt;
  END IF;
END DROP_TABLE;

This procedure can be called with “exec drop_table(‘ff123’); If the table does not exist, we have no entries in user_tables. In that case, nothing happens. If we have a table that exists, the user_table has an entry and the “execute immediate stmt” is executed. This implies the table is dropped.

Door tom