一、查询表中全部的记录个数
可用两种方法,一种是在oracle的系统表中统计,另一种需要写存储过程统计,方法分别如下。
1、系统表中统计:
SELECT sum (num_rows) FROM user_tables;
|
结果:
2、存储过程统计,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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 ;
|
输出结果
二、按照条件查询记录个数
Select Count(*) from tablename where ID>1
三、查询一个用户下所有表的记录总条数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
declare v_count number(10); t_count number(10) default 0;
cursor t_cur is
select table_name
from user_tables;
begin for t_rec in t_cur
loop execute immediate 'select count(*) from ' ||t_rec.table_name|| ' into v_count' ;
t_count:=v_count+t_count; end loop;
dbms_output.put_line(to_char(t_count)); end ;
|
参考文章
1. badkano. 如何在oracle数据库中查询记录总条数。
2. fight2009, owl1230. oracle数据库中如何查询一个用户下所有表的记录总条数。