Search notes:

SQLPATH - drop_if_exists

drop_if_exists.sql is an SQL script to be run in SQL*Plus to drop an object (not only a table) if the object exists.
The script is run like so. Note that the object type is not specified:
@drop_if_exists sales_2018

Script

set verify off
declare
--
--    TODO: Synonyms can have the same name as a table, view, package etc.
--
--    To drop a user/schema, use
--       drop_schema_if_exists.sql
--

  obj_name  varchar2(30) := '&1';
  obj_owner varchar2(30);
  obj_type  varchar2(30);

begin

  select owner, object_type into obj_owner, obj_type
    from (
      select owner, object_type,
             row_number() over (order by case when owner = user then 1 else 2 end) r
        from all_objects
       where object_name = upper(obj_name) and
             object_type not like '% BODY'
    )
    where r = 1;

  execute immediate
    'drop ' || obj_type || ' ' || obj_name || case when obj_type = 'TABLE' then ' purge' end;

exception
   when no_data_found then
        null;

   when others then
        dbms_output.put_line(obj_name || ' caused ' || sqlerrm);

end;
/
Github repository Oracle-SQLPATH, path: /drop_if_exists.sql

See also

The SQL verb drop
The create … if not exists and drop … if exists clause in Oracle 23c.
Oracle: files for ORACLE_PATH / SQLPATH

Index