Oracle Create Table if it does not exist
Normally, it doesn't make a lot of sense to check whether a table exists or not because objects shouldn't be created at runtime and the application should know what objects were created at install time. If this is part of the installation, you should know what objects exist at any point in the process so you shouldn't need to check whether a table already exists.
If you really need to, however,
- You can attempt to create the table and catch the `ORA-00955: name is already used by an existing object" exception.
- You can query
USER_TABLES
(orALL_TABLES
orDBA_TABLES
depending on whether you are creating objects owned by other users and your privileges in the database) to check to see whether the table already exists. - You can try to drop the table before creating it and catch the `ORA-00942: table or view does not exist" exception if it doesn't.
@Archie I would like to answer your question.@Piyas De Sorry for stealing your code :).
Just a little update of @Piyas De answer.
BEGIN BEGIN EXECUTE IMMEDIATE '<<Your table creation Statement>>'; EXCEPTION WHEN OTHERS THEN IF SQLCODE == -955 THEN RAISE; END IF; END;END;