tags:

views:

845

answers:

6

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 and 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.

A: 

Sadly no, there is no such thing as drop if exists, or CREATE IF NOT EXIST

You could write a plsql script to include the logic there.

http://download.oracle.com/docs/cd/B12037_01/server.101/b10759/statements_9003.htm

I'm not much into Oracle Syntax, but i think @Erich's script would be something like this.

declare 
cant integer
begin
select into cant count(*) from dba_tables where table_name='Table_name';
if count>0 then
BEGIN
    DROP TABLE tableName;
END IF;
END;
Tom
Does this even compile?
quillbreaker
+1  A: 

There is no 'DROP TABLE IF EXISTS' in oracle, you would have to do the select statement.

try this (i'm not up on oracle syntax, so if my variables are ify, please forgive me):

declare @count int
select @count=count(*) from all_tables where table_name='Table_name';
if @count>0
BEGIN
    DROP TABLE tableName;
END
Erich
I've made an attempt to translate the script to oracle syntax.
Tom
declare count number;begin select count(*) into count from all_tables where table_name = 'x'; if count > 0 then execute immediate 'drop table x'; end if;end;You cannot run DDL directly from a transaction block, you need to use execute.
Khb
Thanks very much! I hadn't realized the syntax was that different. I DID know you need to wrap the whole thing in a begin/end, but i figured it was being run in the middle of another script.Tom: I decided to leave my version and not copy yours, so I don't take any votes from you, who obviously has the right ansswer.
Erich
+1  A: 
declare
   c int;
begin
   select count(*) into c from user_tables where table_name = upper('table_name');
   if c = 1 then
      execute immediate 'drop table table_name';
   end if;
end;
Marius Burz
A: 

You could always catch the error yourself.

begin
execute immediate 'drop table mytable';
exception when others then null;
end;

It is considered bad practice to overuse this, similar to empty catch()'es in other languages.

Regards
K

Khb
+4  A: 

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 mytable';
EXCEPTION
   WHEN OTHERS THEN
      IF SQLCODE != -942 THEN
         RAISE;
      END IF;
END;
Jeffrey Kemp
A: 

I don't understand why Oracle is so popular. It cannot do a simple if condition properly.

Deepfreezed
Because they created one of the first stable, highly configurable relational databases before such things were available for free online.
Alan Storm
LOL "properly"...
Jeffrey Kemp