我需要放一张桌子并换一张新桌子。如果我删除表并且该表不存在,则会出现错误
如何检查表格是否存在?
我正在研究Oracle 11g
提前致谢。
答案 0 :(得分:7)
你可以这样做:
DECLARE v_exist PLS_INTEGER;
BEGIN
SELECT COUNT(*) INTO v_exist
FROM user_tables
WHERE table_name = 'YOURTABLEHERE';
IF v_exist = 1 THEN
EXECUTE IMMEDIATE 'DROP TABLE YOURTABLEHERE';
END IF;
答案 1 :(得分:4)
DECLARE
eTABLE_OR_VIEW_DOES_NOT_EXIST EXCEPTION;
PRAGMA EXCEPTION_INIT(eTABLE_OR_VIEW_DOES_NOT_EXIST, -942);
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE SCHEMA.WHATEVER';
EXCEPTION
WHEN eTABLE_OR_VIEW_DOES_NOT_EXIST THEN
NULL;
END;
分享并享受。
答案 2 :(得分:1)
类似
select count(*) from user_tables
where table_name= :table name
或
select count(*) from dba_tables
where owner = :table owner
and table_name = :table name
或严厉的替代方案:
begin execute immediate 'drop table table_name';
exception when others then null;
end;
答案 3 :(得分:1)
我一直在使用以下程序来处理这个问题:
create or replace procedure drop_table_if_exists ( p_table_name varchar2 )
is
it_exist number;
begin
select count(*)
into it_exists
from user_tables
where table_name = p_table_name
;
if it_exists >= 1 then
execute immediate 'drop table '||p_table_name;
end if;
end;
/
exec drop_table_if_exists ( 'TABLE_TO_DROP' );