Oracle 過程中檢查資料表存在與否

來源:互聯網
上載者:User

Oracle 過程中檢查資料表存在與否

在過程中,尤其是每天執行的任務,通常要檢查查詢的資料表存在不存在,如果不存在則等待一段時間在進行執行,以下代碼實現了這個功能,如果表不存在,拋出異常,交給異常處理代碼,確保資料完整性

使用方法:p_CheckTable('UserName.TableName')使用者名稱不存在,則在所有表中尋找

create or replace procedure p_CheckTable(p_TableName in varchar2)  as
v_count number;
v_TableName varchar2(200);
v_table varchar2(200);
v_owner varchar2(100);
begin
 v_TableName:=upper(p_TableName);
 v_count:=instr(v_TableName,'.',1,1);
--取owner
 v_owner:=substr(v_TableName,1,v_count-1);
 --dbms_output.put_line(v_owner);

--get table name 
 v_table:=substr(v_TableName,v_count+1,length(v_TableName)-v_count);
 --dbms_output.put_line(v_table);
 
 --if not use other user table ,the owner string is null,then check all tables
 if v_owner is null then
  select count(*) into v_count from all_tables a where a.TABLE_NAME=v_table;
 else
  select count(*) into v_count from all_tables a where a.TABLE_NAME=v_table and  owner=v_owner;
 end if;
 if v_count=0 then
  raise_application_error(-20010,p_TableName||' is not exist,Please wait..');
 end if;
 
end p_CheckTable;

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.