MySQL database has syntax to prevent error on create table
if table already exists:
create table if not exist "TBL" (...);
Unfortunately Oracle database doesn't have such syntax. It can be mimicked with PL/SQL code block and exception handling. Because PL/SQL code doesn't allow DDL code dynamic queries are used:
declare begin execute immediate 'create table "TBL" ("ID" number not null)'; exception when others then null; end; /
More safer implementation checks for error code:
ORA-00955: name is already used by an existing object
which same for create table
, and create index
, and create sequence
, etc and report error
otherwise. Final solution looks like:
declare begin execute immediate ' create table "TBL" ("ID" number not null)'; exception when others then if SQLCODE = -955 then null; else raise; end if; end; /