一、查询表中全部的记录个数
可用两种方法,一种是在oracle的系统表中统计,另一种需要写存储过程统计,方法分别如下。
1、系统表中统计:
SELECT sum (num_rows) FROM user_tables; |
结果:
2、存储过程统计,代码如下:
declarev_tName varchar(50);v_sqlanalyze varchar(500);v_num number;v_sql varchar(500);cursor c1 isselect table_name from user_tables;beginopen c1;loopfetch 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);elseexit;end if;end loop;end;
输出结果
二、按照条件查询记录个数
Select Count(*) from tablename where ID>1
三、查询一个用户下所有表的记录总条数
declarev_count number(10);t_count number(10) default 0;cursor t_cur isselect table_namefrom user_tables;beginfor t_rec in t_curloopexecute 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. . 。
2. , . 。