2011-06-15 67 views

回答

1

Oracle有一个all_tables表,你可以查询看看。

也许是这样的:

declare 
    v_tab_count number := 0; 
begin 
    select count(*) 
    into v_tab_count 
    from all_tables 
    where table_name = 'MY_TABLE'; 

    if v_tab_count > 0 then 
     execute immediate 'drop table my_table'; 
    else 
     dbms_output.put_line('The table isn''t there! maybe you deleted it already?'); 
    end if; 
exception 
    when others then 
     dbms_output.put_line(sqlerrm); 
end if; 
/

我知道我刚才在别人的帖子评论说,我不喜欢使用execute immediate这一点,但我忘了这是执行drop table的唯一途径来自PL/SQL。

+0

太好了! User_Tables和All_tables(如果2个用户具有相同的表,则计数可以为2)使用User_tables会更好吗? – kalls 2011-06-15 17:56:44

+0

@ kalls:你可以在all_tables或user_tables的查询中添加一些额外的条件? – FrustratedWithFormsDesigner 2011-06-15 18:10:24

1

您可以查询USER_TABLES TABLE_NAME为= 'YOUR_TABLE_NAME' :-)

+0

感谢您的输入! – kalls 2011-06-15 17:57:18

2

http://www.dba-oracle.com/bk_check_table_exists.htm

这里有给出不同的解决方案。最简单的:

SQL>递减MYTABLE

或者只是尝试删除并捕获异常:

begin 
execute immediate 'drop table TABLE1'; 
exception when others then null; 
end; 
+0

DROP替代方案真的很糟糕,忽略了一个显而易见的事实,即想知道表是否存在是最常见的事情,因为它想要拥有它! – Mirvnillith 2012-12-20 06:47:15