查询以查找模式名称HR中的空表数

时间:2017-10-27 09:39:46

标签: oracle

我想在我的架构hr中找到空表的数量,并且我已经为它编写了代码:

set serveroutput on;


Declare
d Number;
c varchar2(25);
cursor c1 is SELECT DISTINCT OWNER, OBJECT_NAME FROM DBA_OBJECTS WHERE OBJECT_TYPE = 'TABLE' AND OWNER = 'HR';

Begin
 for r in c1
Loop
select count(*) into d
from (r.object_name);
if (d = 0) then
dbms_output.put_line(r.object_name||'is Empty');
end if;
end loop;
end;

但它在(r.object_name)的这一行抛出错误。任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:3)

查询dba_tables.num_rows不可靠,因为这是估算值。如果从未对表进行过分析,则可能无法反映正确的行数。

此处也不需要动态SQL。基于计算所有表的行数的my answer,您只需添加where条件:

select table_name
from (
  select table_name, 
         to_number(extractvalue(xmltype(dbms_xmlgen.getxml('select count(*) c from '||owner||'.'||table_name)),'/ROWSET/ROW/C')) as row_count
   from all_tables
   where owner = 'HR'
)
where row_count = 0;

答案 1 :(得分:1)

您需要dynamic SQLexecute immediate):

declare
  d number;
  v_sql varchar2(1000);

  cursor c1 is select object_name 
    from all_objects 
    where object_type = 'TABLE' and owner = 'HR';

begin
  for r in c1 loop
      v_sql := 'select count(*) from HR.'||r.object_name;
      execute immediate v_sql into d;
      if d = 0 then
          dbms_output.put_line(r.object_name||' is empty');
      end if;
  end loop;
end;

答案 2 :(得分:0)

在Oracle中,您可以使用以下查询。

select    count(*) from    dba_tables  where    OWNER = 'XXXX' and num_rows =0;