oracle: drop table if exists
pl/sql not working using exception. e.g.,
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
SQL> EXECUTE IMMEDIATE 'DROP TABLE Foo';
SQL> EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(sqlerrm);
SQL> END;
SQL> /
The Foo table is not dropped.
create table Foo (id number(20,0), name varchar(20),
primary key(id));
create table Bar (id number(20,0), name varchar(20),
primary key(id),
constraint FK1 foreign key (id) references Foo (id));
There is a FK constraint.
SET CONSTRAINTS ALL DEFERRED;
BEGIN EXECUTE IMMEDIATE 'DROP TABLE Foo'; EXCEPTION WHEN OTHERS THEN NULL; END;
/
SET CONSTRAINTS ALL IMMEDIATE;
The table Foo is not deleted.