Oracle查詢資料庫中所有表的記錄數

來源:互聯網
上載者:User
方法一:
首先建立一個計算函數
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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
擷取要統計的值
方法二:
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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.