簡體   English   中英

哪種方法適合檢查表是否存在

[英]which way is appropriate to check table existence

我想檢查Oracle中特定的表是否存在,哪種方法更通用,更合適,我在下面列出了2種方法,如果表存在,則方法1可以快速運行,因為它只運行一個sql

  1. 處理異常並了解它。

     create or replace procedure get_id_only (id out number) as begin execute immediate 'SELECT id FROM TABLE_NAME where rownum = 1' into id; exception when others then if (sqlcode = -942) then SELECT id into id FROM my_another_table; else raise; end if; end; 
  2. 檢查用戶表以查看它是否存在。

     create or replace procedure get_id_only (id out number) as count number; begin SELECT count(*) into count FROM user_tables WHERE table_name = 'TABLE_NAME'; if (count = 0) then SELECT id into id FROM my_another_table; return; end if; execute immediate 'SELECT id FROM TABLE_NAME where rownum = 1' into id; end; 

如您所說,第一個是最好,最有效的方法。 因為捕獲了“找不到表”異常:這樣可以避免檢查表是否存在兩次的開銷;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM