drop table if exists oracle code example

Example 1: oracle drop table if exists

BEGIN
   EXECUTE IMMEDIATE 'DROP TABLE ' || table_name;
EXCEPTION
   WHEN OTHERS THEN
      IF SQLCODE != -942 THEN
         RAISE;
      END IF;
END;

Example 2: oracle sql drop column if exists

DECLARE
  l_cnt NUMBER;
BEGIN
  SELECT COUNT(*)
    INTO l_cnt 
    FROM dba_tab_columns
   WHERE owner = 'my_owner'
     AND table_name = 'my_table'
     AND column_name = 'my_column';
  IF( l_cnt = 1 ) THEN
    EXECUTE IMMEDIATE 'ALTER TABLE my_table DROP COLUMN my_column';
  END IF;
END;

Tags:

Sql Example