Oracle之 在創建Table之前先判斷是否存在,存在就刪除

create or replace function Func_Is_Exists_Table
(
  i_table_name in varchar2
) 
return number 
is
  o_result number;
begin
  declare
    num number;
  begin
    select count(1) into num 
    from user_tables 
    where table_name = upper(i_table_name) 
      or table_name = lower(i_table_name);
    
    if num > 0 then
      o_result := 1;
    end if;
    if num <= 0 then
      o_result := 0;
    end if;
  end;
  return o_result;
end Func_Is_Exists_Table;
/

CREATE OR REPLACE PROCEDURE Proc_Check_Exist_Table
(
  i_table_name IN VARCHAR2
)
AS
BEGIN
  BEGIN
    IF Func_Is_Exists_Table(i_table_name) > 0 THEN
      dbms_output.put_line('Table-->> ' || upper(i_table_name) || ' <<--exists');
      execute immediate 'drop table ' || i_table_name;  
    ELSE
      dbms_output.put_line('Table-->> ' || upper(i_table_name) || ' <<--not exists');
    END IF;
  END;
END Proc_Check_Exist_Table;
/ 

exec proc_check_exist_table('rel_ppv_subscription');
create table rel_ppv_subscription
(
  subscription_no number(1,1) not null,--訂購序列號,來自於sequence:
  subscription_time date not null,--訂購時間
  unsubscription_time date,--退訂時間
  apply_time date,--生效時間
  expire_time date,--失效時間
  subscriber_id varchar2(50) not null,--訂戶id
  product_id varchar2(50) not null,--產品id
  content_id varchar2(50) not null,--內容id
  content_type number(1,1),--內容類型,取值:
  area_id varchar2(50),--區域id
  subnet_id varchar2(50),--子網id
  domain_id varchar2(50)--領域id
);

---直接執行
declare  
    num number;  
  begin  
    select count(1) into num   
    from user_tables   
    where table_name = upper('Table_name')   
      or table_name = lower('Table_name');  
      
    if num > 0 then   
      execute immediate 'drop table Table_name';
 end if; end;
/
create table Table_name(id varchar2(100));
發佈了28 篇原創文章 · 獲贊 6 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章