在Oracle中删除表格(如果存在)(IF EXIST)
在Oracle中删除表格(如果存在)(IF EXIST)
这个问题已经在这里有了答案:
我正在使用 Oracle 12c,如果我要删除表“CONTINENT”,而该表不存在,我不想出现错误。
我做了这个
set echo on set serveroutput on alter session set current_schema=WORK_ODI; set verify off set pause off begin execute immediate 'drop table continent'; exception when others then null; end;
这个脚本对我很有效。我也使用这个脚本:
declare c int; begin select count(*) into c from user_tables where table_name = upper('continent'); if c = 1 then execute immediate 'drop table continent'; end if; end;
这两个脚本都很有效,但是我的老板想要像 IF EXIT
这样的东西。谁能帮帮我怎么在这种情况下使用 IF EXIT?
admin 更改状态以发布 2023年5月23日
你可以做两件事情
-
定义你想要忽略的异常(例如ORA-00942)
-
添加一个未记录(也未实现)的提示/*+ IF EXISTS */,这会让你的管理层高兴。
。
declare table_does_not_exist exception; PRAGMA EXCEPTION_INIT(table_does_not_exist, -942); begin execute immediate 'drop table continent /*+ IF EXISTS */'; exception when table_does_not_exist then DBMS_OUTPUT.PUT_LINE('Ignoring table or view does not exist') ; end; /
附加说明:使用
exception when others then null;
可能是危险的,例如当表不被删除时,你也会忽略表空间离线等错误。