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

來源:互聯網
上載者:User

標籤:

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

使用方法: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;

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.