Create table if not exist with exceptions 1

Create table if not exist with exceptions

declare
v_sql LONG;
begin

v_sql:='create table EMPLOYEE
  (
  ID NUMBER(3),
  NAME VARCHAR2(30) NOT NULL
  )';
execute immediate v_sql;

EXCEPTION
    WHEN OTHERS THEN
      IF SQLCODE = -955 THEN
        NULL; -- suppresses ORA-00955 exception
      ELSE
         RAISE;
      END IF;
END; 
/

Here is what the above code is Doing:
1. Declare a variable v_sql of type LONG.
2. Assign the CREATE TABLE statement to the variable v_sql.
3. Execute the statement using the EXECUTE IMMEDIATE statement.
4. If the table already exists, the exception ORA-00955 is raised.
5. The exception is suppressed by the IF statement.
6. If any other exception is raised, it is re-raised.

Similar Posts