Can anyone point me to the right syntax to use in order to create a table only if it does not currently exist in the database?
I'm currently programming a Java GUI in order to connect to Oracle and execute statements on my database and I'm wondering if I would implement this as a Java constraint or a SQLPlus constraint.
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,
USER_TABLES
(or ALL_TABLES
or DBA_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 do this with the Following Procedure -
BEGIN
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE <<Your Table Name>>';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
EXECUTE IMMEDIATE '<<Your table creation Statement>>';
END;
Hope this may help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With