有没有办法在 Oracle 中查看哪些表有数据,哪些表没有?

Is there a way to in Oracle to see what tables have data and which don't?

我想知道是否有一种方法可以搜索数据库并找出哪些表是空的,哪些表有数据。我将把一些数据迁移到另一个系统,如果知道我应该导出哪些表就好了。我正在使用 Oracle SQL 开发人员。

除了为每个 table 设置一个愚蠢的 pl/sql 块到 count(*) 之外,一种方法是 运行 这个:

SELECT num_rows FROM ALL_TAB_STATISTICS WHERE OWNER = 'user name';

(替代 tables: DBA_TAB_STATISTICS, USER_TAB_STATISTICS)

但是,仅当您最近使用 DBMS_STATS 包收集统计数据时才有效。

是的,您可以 select 使用类似

的查询对数据库中的所有表进行计数
select table_name,
to_number(
   extractvalue(
      xmltype(
         dbms_xmlgen.getxml('select count(*) c from '||table_name))
,'/ROWSET/ROW/C')) count
from user_tables;

这里是demo

接受的答案不正确,因为需要在最后一次 table 修改之后收集统计信息,以保证查询将产生正确的结果。

Shreyas Chavan 的回答是正确的,但性能可能很差。以下查询给出了准确的答案,但在有很多 table 的情况下应该表现更好。

select owner, table_name,
case
to_number(
   extractvalue(
      xmltype(
         dbms_xmlgen.getxml('select count(1) c 
                             from '||owner||'.'||table_name||' 
                             where rownum<2'))
,'/ROWSET/ROW/C')) 
when 1 then 'Y' else 'N' END has_data
from all_tables 
ORDER BY owner, table_name;