Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle: If Table Exists

I'm writing some migration scripts for an Oracle database, and was hoping Oracle had something similar to MySQL's IF EXISTS construct.

Specifically, whenever I want to drop a table in MySQL, I do something like

DROP TABLE IF EXISTS `table_name`; 

This way, if the table doesn't exist, the DROP doesn't produce an error, and the script can continue.

Does Oracle have a similar mechanism? I realize I could use the following query to check if a table exists or not

SELECT * FROM dba_tables where table_name = 'table_name'; 

but the syntax for tying that together with a DROP is escaping me.

like image 415
Alan Storm Avatar asked Nov 25 '09 18:11

Alan Storm


People also ask

How do you check if a table exists in Oracle?

You can also check the data dictionary to see if a table exists: SQL> select table_name from user_tables where table_name='MYTABLE'; Another way to test if a table exists is to try to drop the table and catch the exception if it does not exist. and include the URL for the page.

How do I check if a table exists?

To check if table exists in a database you need to use a Select statement on the information schema TABLES or you can use the metadata function OBJECT_ID(). The INFORMATION_SCHEMA. TABLES returns one row for each table in the current database.

Can we use if exists in Oracle?

The Oracle EXISTS condition is used in combination with a subquery and is considered "to be met" if the subquery returns at least one row. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

How do you drop a table only if it exists in Oracle?

DROP TABLE IF EXISTS `table_name`; This way, if the table doesn't exist, the DROP doesn't produce an error, and the script can continue. SELECT * FROM dba_tables where table_name = 'table_name'; but the syntax for tying that together with a DROP is escaping me.


1 Answers

The best and most efficient way is to catch the "table not found" exception: this avoids the overhead of checking if the table exists twice; and doesn't suffer from the problem that if the DROP fails for some other reason (that might be important) the exception is still raised to the caller:

BEGIN    EXECUTE IMMEDIATE 'DROP TABLE ' || table_name; EXCEPTION    WHEN OTHERS THEN       IF SQLCODE != -942 THEN          RAISE;       END IF; END; 

ADDENDUM For reference, here are the equivalent blocks for other object types:

Sequence

BEGIN   EXECUTE IMMEDIATE 'DROP SEQUENCE ' || sequence_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -2289 THEN       RAISE;     END IF; END; 

View

BEGIN   EXECUTE IMMEDIATE 'DROP VIEW ' || view_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -942 THEN       RAISE;     END IF; END; 

Trigger

BEGIN   EXECUTE IMMEDIATE 'DROP TRIGGER ' || trigger_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -4080 THEN       RAISE;     END IF; END; 

Index

BEGIN   EXECUTE IMMEDIATE 'DROP INDEX ' || index_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -1418 THEN       RAISE;     END IF; END; 

Column

BEGIN   EXECUTE IMMEDIATE 'ALTER TABLE ' || table_name                 || ' DROP COLUMN ' || column_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -904 AND SQLCODE != -942 THEN       RAISE;     END IF; END; 

Database Link

BEGIN   EXECUTE IMMEDIATE 'DROP DATABASE LINK ' || dblink_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -2024 THEN       RAISE;     END IF; END; 

Materialized View

BEGIN   EXECUTE IMMEDIATE 'DROP MATERIALIZED VIEW ' || mview_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -12003 THEN       RAISE;     END IF; END; 

Type

BEGIN   EXECUTE IMMEDIATE 'DROP TYPE ' || type_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -4043 THEN       RAISE;     END IF; END; 

Constraint

BEGIN   EXECUTE IMMEDIATE 'ALTER TABLE ' || table_name             || ' DROP CONSTRAINT ' || constraint_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -2443 AND SQLCODE != -942 THEN       RAISE;     END IF; END; 

Scheduler Job

BEGIN   DBMS_SCHEDULER.drop_job(job_name); EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -27475 THEN       RAISE;     END IF; END; 

User / Schema

BEGIN   EXECUTE IMMEDIATE 'DROP USER ' || user_name;   /* you may or may not want to add CASCADE */ EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -1918 THEN       RAISE;     END IF; END; 

Package

BEGIN   EXECUTE IMMEDIATE 'DROP PACKAGE ' || package_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -4043 THEN       RAISE;     END IF; END; 

Procedure

BEGIN   EXECUTE IMMEDIATE 'DROP PROCEDURE ' || procedure_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -4043 THEN       RAISE;     END IF; END; 

Function

BEGIN   EXECUTE IMMEDIATE 'DROP FUNCTION ' || function_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -4043 THEN       RAISE;     END IF; END; 

Tablespace

BEGIN   EXECUTE IMMEDIATE 'DROP TABLESPACE ' || tablespace_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -959 THEN       RAISE;     END IF; END; 

Synonym

BEGIN   EXECUTE IMMEDIATE 'DROP SYNONYM ' || synonym_name; EXCEPTION   WHEN OTHERS THEN     IF SQLCODE != -1434 THEN       RAISE;     END IF; END; 
like image 57
Jeffrey Kemp Avatar answered Sep 21 '22 14:09

Jeffrey Kemp