IF EXISTS(SELECT name FROM sys.tables WHERE name='TABLE_NAME')
DROP TABLE TABLE_NAME
-- The rest of the code goes here
What's the equivalent Oracle code? BEGIN
EXECUTE IMMEDIATE 'drop table table_name';
--EXECUTE IMMEDIATE 'drop table table_name purge'; --- purge prevents saving it in the recyclebin
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line(SQLERRM);
END;
DECLARE
not_exist EXCEPTION;
PRAGMA EXCEPTION_INIT(not_exist, -942);
BEGIN
...
BEGIN
EXECUTE IMMEDIATE 'drop table table_name';
EXCEPTION
WHEN not_exist THEN
NULL;
END;
....
END;
I don't like to use WHEN OTHERS because that could ignore errors that you really shouldn't be ignoring. Ignore the message that you want to ignore.
@delimiter ++;
declare v_exist number(1);
SELECT COUNT(*) INTO v_exist FROM user_tables WHERE table_name = 'FOO';
if v_exist = 1 then
execute immediate 'drop table FOO';
end if;
CREATE TABLE FOO (bar number(1));
SELECT bar FROM FOO;
14:43:01 [@DELIMITER - 0 row(s), 0.000 secs] Command processed
14:43:01 [DECLARE - 0 row(s), 0.071 secs] [Error Code: 6550, SQL State: 65000] ORA-06550: line 3, column 1:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
The symbol "begin" was substituted for "SELECT" to continue.
ORA-06550: line 9, column 1:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
( begin case declare end exception exit for goto if loop mod
null pragma raise return select update while with
<an identifier> <a double-quoted delimited-identifier>
<a bind variable> << continue close current delete fetch lock
insert open rollback savepoint set sql execute commit forall
merge pipe purge
... 2 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.071/0.000 sec [1 successful, 0 warnings, 1 errors]
declare
v_exist number(1);
begin
SELECT...
@delimiter ++;
declare v_exist number(1);
begin
SELECT COUNT(*) INTO v_exist FROM user_tables WHERE table_name = 'FOO';
if v_exist = 1 then
execute immediate 'drop table FOO';
end if;
end;
execute immediate 'CREATE TABLE FOO (bar number(1))';
SELECT bar FROM FOO;
14:49:38 [@DELIMITER - 0 row(s), 0.000 secs] Command processed
14:49:38 [DECLARE - 0 row(s), 0.071 secs] [Error Code: 6550, SQL State: 65000] ORA-06550: line 13, column 9:
PLS-00103: Encountered the symbol "IMMEDIATE" when expecting one of the following:
;
... 2 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.071/0.000 sec [1 successful, 0 warnings, 1 errors]
@delimiter ++;
declare v_exist number(1);
begin
SELECT COUNT(*) INTO v_exist FROM user_tables WHERE table_name = 'FOO';
if v_exist = 1 then
execute immediate 'drop table FOO';
end if;
execute immediate 'CREATE TABLE FOO (bar number(1))';
end;
SELECT bar FROM FOO;
Just drop it and ignore the message that it didn't exist in the first place.
For logic, you need to be in PL/SQL and you cannot to DDL natively in there so you need to use the execute immediate like you have in other questions.
Just execute the drop and trap the exception?
Like in your previous question: You could select from user_tables/all_tables/dba
Now the bigger question:
Why drop the table in the first place? When questions come into Oracle from other database products and I see "drop table" in "code", I think temp tables. You really shouldn't do that in Oracle. There are other ways...