考慮到我有一個名爲SBST的架構查詢查找所有空表
我想查找此SBST架構中的所有空表列表。有沒有任何PL/SQL程序來找到它。我發現很少。但是那些使用用戶表的地方我無法指定Schema名稱SBST。
我就是用這個
select table_name from dba_tables where owner ='SBST'
having count(*)=0 group by table_name
什麼是錯在上面的查詢?
考慮到我有一個名爲SBST的架構查詢查找所有空表
我想查找此SBST架構中的所有空表列表。有沒有任何PL/SQL程序來找到它。我發現很少。但是那些使用用戶表的地方我無法指定Schema名稱SBST。
我就是用這個
select table_name from dba_tables where owner ='SBST'
having count(*)=0 group by table_name
什麼是錯在上面的查詢?
類似於@ shareef的回答,但使用dynamic SQL來避免必須創建臨時文件.sql
。您需要dbms_output
才能看到,例如與SQL * Plus中的set serveroutput on
- 不知道關於蟾蜍。
declare
cursor c(p_schema varchar2) is
select 'select ''' || table_name || ''' from ' ||
p_schema ||'.' || table_name || ' where rownum < 2 ' ||
' having count(*) = 0' as query
from all_tables
where owner = p_schema
order by table_name;
l_table all_tables.table_name%TYPE;
begin
for r in c('SBST') loop
begin
execute immediate r.query into l_table;
exception
when no_data_found then continue;
end;
dbms_output.put_line(l_table);
end loop;
end;
/
使用all_tables
所以你知道你可以從它列出的表中選擇似乎比dba_tables
這裏更有用。我還在from
子句中包含了模式,以防其他用戶擁有同名的表,因此,如果您作爲不同用戶連接,您仍然可以看到它 - 可能也避免了同義詞問題。
具體有什麼錯查詢......你有having
和group by
周圍子句中的錯誤的方式;但它總是不會返回任何數據,因爲如果SBST有任何表,那麼count (*) from dba_tables
必須非零,因此having
總是匹配;如果沒有,那麼也沒有數據,所以having
沒有什麼可以匹配的。你在計算有多少表,而不是每個表中有多少行。
的直截了當的答案是
select 'select ''' || table_name || ''' from ' || table_name || '
having count(*) = 0;' from dba_tables where owner='SBST';
說明
您可以運行這個...這將只輸出那些沒有行的表名: 假設你使用sqlplus中,但我使用蟾蜍對其進行測試 並且它工作得很好 設置回顯標題關閉反饋關閉行100頁0;
spool tmp.sql
select 'select ''' || table_name || ''' from ' || table_name || '
having count(*) = 0;' from user_tables where owner='SBST';
spool off;
@tmp.sql
如果你打開「tmp.sql」文件,你會看到所有表....
select 'PERSONS' from PERSONS having count(*) = 0;
select 'DEPARTMENT' from DEPARTMENT having count(*)=0;
你的情況
你想有一個架構和模式是正確的用戶上面的代碼,如果你與用戶SBST連接,但如果你與其他連接,那麼你必須使用DBA_TABLES和分配所有者屬性SBST
USER_TABLES是你自己ALL_TABLES是自己的, 和其他用戶,您已被授予DBA_TABLES excplicit 訪問表所有者表是數據庫中的所有表的表
這樣
set echo off heading off feedback off lines 100 pages 0;
spool tmp.sql
select 'select ''' || table_name || ''' from ' || table_name || '
having count(*) = 0;' from dba_tables where owner='SBST';
spool off;
@tmp.sql
這三種方法都享有基本SYS表,但USER_和ALL_ 意見,您的用戶名/安全信息joing限制結果
**摘要**
請你只運行此查詢
select 'select ''' || table_name || ''' from ' || table_name || '
having count(*) = 0;' from dba_tables where owner='SBST';
@peter有什麼辦法在查詢中直接指定SBST。我在Toad.Datswhy中執行 – user472625
我在編輯'select'中添加了上面的選擇'''|| table_name || '''來自'|| table_name || 'count(*)= 0;' ';'來自dba_tables,其中owner ='SBST'; ' – shareef
@peter:上面列出了tablename和count.This意味着我想複製一個文件中的所有結果並執行this.Is是否有任何查詢? – user472625
您可以觸發以下查詢查找表的名單沒有巡航能力數據
select * from ALL_TABLES
where owner ='SBST'
AND NUM_ROWS = 0;
如果表統計數據是最新的,那麼你可以使用:
SELECT TABLE_NAME
FROM ALL_TAB_STATISTICS
WHERE (OWNER = 'ME')
AND (NUM_ROWS = 0);
'擁有'條款是沒有必要的。 'SELECT 1 FROM owner.table WHERE rownum <2'足以確定表中是否至少有一行。 – ipip
@ipip - 是的,這對我的數據來說稍微快一些,其中包括一些合理的大表;但是會給你帶有數據的表,而OP則希望那些沒有數據的表。 –
不,我在說垃圾;如果我在實際上具有重要數據的數據庫上運行它,速度會更快,正如您所期望的那樣。但它確實列出了包含數據的表格;儘管這兩個條款結合起來似乎有所斬獲。謝謝。 –