Oracle查询数据库中所有表的记录数
方法一:
首先建立一個計算函數(shù)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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;
然后通過計算函數(shù)進行統(tǒng)計
select table_name, count_rows(table_name) nrows from user_tables
獲取要統(tǒng)計的值
方法二:
select t.table_name,t.num_rows from user_tables t
查看記錄數(shù),但是num_rows存儲的是上次分析后的值,不準(zhǔn)確,要使用該方法,必須分析后才可以試用
完成的語句為
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||' 行數(shù): '||v_num);
else
exit;
end if;
end loop;
end;
轉(zhuǎn)載于:https://www.cnblogs.com/langtianya/p/6526415.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的Oracle查询数据库中所有表的记录数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人造卫星为什么会绕着地球转而不是停在太空
- 下一篇: mvc EF 从数据库更新实体,添加视图