2011-06-15 65 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