參考https://stackoverflow.com/questions/1799128/oracle-if-table-exists 我的官方博客http://blog.alei.tech ,轉載請注明。網頁地址https://alei.tech/2016/08/12/%E5%9C%A8Oracle%E6%95%B0%E6%8D%AE%E4%B8%AD%E5%AF%BB%E6%89%BE%E4%B8%80%E4%B8%AA%E5%80%BC/
場景 刪除表,視圖等對象時,靜默執行,不返回報錯信息 類似dorp table if exists,語句可反復執行 開發人員編寫sql,讓實施人員執行 直接寫drop table abc,如果abc表已經被刪除或者不存在,返回報錯信息,對於不懂sql的實施人員來說,會產生干擾
代碼示例 創建存儲過程 適用於drop table, procedure, function, trigger, view, sequence
create or replace procedure dropObject(ObjName varchar2,ObjType varchar2) is v_counter number := 0; begin if upper(ObjType) = 'TABLE' then select count(*) into v_counter from user_tables where table_name = upper(ObjName); if v_counter > 0 then execute immediate 'drop table ' || ObjName || ' cascade constraints'; end if; end if; if upper(ObjType) = 'PROCEDURE' then select count(*) into v_counter from User_Objects where object_type = 'PROCEDURE' and OBJECT_NAME = upper(ObjName); if v_counter > 0 then execute immediate 'DROP PROCEDURE ' || ObjName; end if; end if; if upper(ObjType) = 'FUNCTION' then select count(*) into v_counter from User_Objects where object_type = 'FUNCTION' and OBJECT_NAME = upper(ObjName); if v_counter > 0 then execute immediate 'DROP FUNCTION ' || ObjName; end if; end if; if upper(ObjType) = 'TRIGGER' then select count(*) into v_counter from User_Triggers where TRIGGER_NAME = upper(ObjName); if v_counter > 0 then execute immediate 'DROP TRIGGER ' || ObjName; end if; end if; if upper(ObjType) = 'VIEW' then select count(*) into v_counter from User_Views where VIEW_NAME = upper(ObjName); if v_counter > 0 then execute immediate 'DROP VIEW ' || ObjName; end if; end if; if upper(ObjType) = 'SEQUENCE' then select count(*) into v_counter from user_sequences where sequence_name = upper(ObjName); if v_counter > 0 then execute immediate 'DROP SEQUENCE ' || ObjName; end if; end if; end;
使用存儲過程 創建temp_table表之前,判斷,if exists then drop
此語句示例在pl/sql developer中執行 ”/”必須在行首,之前不能有空格 begin dropObject('temp_table','table'); end; / create table ———————————————— 版權聲明:本文為CSDN博主「lemon_shenzhen」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/lemon_shenzhen/article/details/79022882