Oracle查询数据库中所有表的记录数

 

方法一:
首先建立一个计算函数

create or replace function count_rows(table_name in varchar2,
                              owner in varchar2 default null)
return number
authid current_user
IS
   num_rows number;
   stmt varchar2(2000);
begin
   if owner is null then
      stmt := 'select count(*) from "'||table_name||'"';
   else
      stmt := 'select count(*) from "'||owner||'"."'||table_name||'"';
   end if;
   execute immediate stmt into num_rows;
   return num_rows;
end;

然后通过计算函数进行统计
select table_name, count_rows(table_name) nrows from user_tables order by     count_rows(table_name) desc
获取要统计的值
方法二:
select t.table_name,t.num_rows from user_tables t 
查看记录数,但是num_rows存储的是上次分析后的值,不准确,要使用该方法,必须分析后才可以试用
完成的语句为

 

 

declare
v_tName varchar(50);
v_sqlanalyze varchar(500);
v_num number;
v_sql varchar(500);
cursor c1 
is
select table_name from user_tables;
begin
open c1;
loop
fetch c1 into v_tName;
if c1%found then

v_sqlanalyze :='analyze table  '||v_tName||'  estimate statistics';
execute immediate v_sqlanalyze;
v_sql := 'select NUM_ROWS  from user_tables where table_name =upper('''||v_tName||''')';

execute immediate v_sql into  v_num;
---dbms_output.put_line('表名: '||v_tName||' 行数: '||v_num);
else
exit;
end if;
end loop;
end;

 

 

你可能感兴趣的:(Oracle查询数据库中所有表的记录数)