Home » SQL & PL/SQL » SQL & PL/SQL » get row counts of tables in schema without using dba_tables.num_rows (Oracle 10g)
get row counts of tables in schema without using dba_tables.num_rows [message #470404] Mon, 09 August 2010 15:08 Go to next message
jxh461
Messages: 185
Registered: March 2005
Senior Member
Hi,

I am trying to get a row count(*) for all the tables in my schema. The NUM_ROWS column in DBA_TABLES is not appropriate in this case because they are as good as the last analyze. So I need to get real time counts.

I tried the following code but I can't seem to catch my error.

DECLARE

 l_sql varchar2(150);
 
 cursor tablelist is 
 select table_name from dba_tables where owner = 'ME';
 

 tabs tablelist%rowtype;
 n_rowcnt number;
 
Begin
 open tablelist;
      loop
          fetch tablelist into tabs;
          exit when tablelist%notfound;
          
          l_sql := 'select count(*) ROW_COUNT from '||tabs||' ;';
          
          n_rowcnt := l_sql;       
          dbms_output.put_line(tabs,n_rowcnt);
      end loop;
 close tablelist;
end;


I am getting the following error:

ORA-06550: line 18, column 20:
PLS-00306: wrong number or types of arguments in call to '||'
ORA-06550: line 18, column 11:
PL/SQL: Statement ignored
ORA-06550: line 21, column 11:
PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'
ORA-06550: line 21, column 11:
PL/SQL: Statement ignored


My expected results are :

TABLE_NAME ROW_COUNT
---------- ----------


Please help or advise if there is a better way.
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470406 is a reply to message #470404] Mon, 09 August 2010 15:23 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
wrong type in line 18
wrong count in line 20
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470407 is a reply to message #470406] Mon, 09 August 2010 15:25 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
"tabs" probably shouldn't be declared as ROWTYPE, but "column%type". Also, you should EXECUTE IMMEDIATE select statement you prepared - you can't just say it the way you did. Also, a prepared statement shouldn't end with a semi-colon.

A slightly modified code looks like this:
DECLARE

  l_sql varchar2(150);
 
  cursor tablelist is 
    select table_name from user_tables; -- where owner = 'ME';
 
  tabs user_tables.table_name%type;

  n_rowcnt number;
 
Begin
  open tablelist;
    loop
       fetch tablelist into tabs;
       exit when tablelist%notfound;
          
       l_sql := 'select count(*) ROW_COUNT from '||tabs;

       execute immediate l_sql into n_rowcnt;
       dbms_output.put_line(tabs||' '||n_rowcnt);
     end loop;
  close tablelist;
end;
/
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470419 is a reply to message #470407] Mon, 09 August 2010 17:39 Go to previous messageGo to next message
jxh461
Messages: 185
Registered: March 2005
Senior Member
Thanks Littlefoot. This worked perfectly.
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470453 is a reply to message #470404] Tue, 10 August 2010 01:08 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
SCOTT> select table_name,
  2         to_number(extractvalue(
  3                     dbms_xmlgen.getXMLtype ('select count(*) cnt from '||table_name),
  4                     '/ROWSET/ROW/CNT')) rows_in_table
  5  from user_tables
  6  where tablespace_name is not null or partitioned='YES' 
  7  order by 1
  8  /
TABLE_NAME                     ROWS_IN_TABLE
------------------------------ -------------
BONUS                                      0
DEPT                                       4
EMP                                       14
SALGRADE                                   5

4 rows selected.

Regards
Michel
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470457 is a reply to message #470453] Tue, 10 August 2010 01:13 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Awesome Michel, but does it provide real time data ( Just Asking !!! Smile )?
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470459 is a reply to message #470457] Tue, 10 August 2010 01:16 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
/forum/fa/5263/0/
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470478 is a reply to message #470457] Tue, 10 August 2010 01:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Yes it is real time data, it executes the embedded query.

Regards
Michel
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470488 is a reply to message #470478] Tue, 10 August 2010 02:09 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Smile Thanks.
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470502 is a reply to message #470488] Tue, 10 August 2010 02:25 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
@rahulvb: my smiley was about Michel's query, not your question.
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470505 is a reply to message #470502] Tue, 10 August 2010 02:28 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Littlefoot wrote on Tue, 10 August 2010 02:25
@rahulvb: my smiley was about Michel's query, not your question.




Oh Ok my apology LittleFoot ..That why we have quote Smile
AND

Even I was quite Surprise to see that I have Book marked that topic too Very Happy
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470513 is a reply to message #470505] Tue, 10 August 2010 02:46 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
I have Tested Michel's Query And Indeed its very fast compare to Conventional Count(*) method.
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470524 is a reply to message #470513] Tue, 10 August 2010 03:19 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
The difference is only in the number of roundtrips between client and server, so not so much fast with big tables but really faster when you have a bunch of small tables.

Regards
Michel
Re: get row counts of tables in schema without using dba_tables.num_rows [message #470536 is a reply to message #470524] Tue, 10 August 2010 03:37 Go to previous messageGo to next message
halim
Messages: 100
Registered: September 2008
Senior Member


Michel is real genius and different. thanks

Thanks
Halim
Re: get row counts of tables in schema without using dba_tables.num_rows [message #604125 is a reply to message #470453] Tue, 24 December 2013 04:50 Go to previous messageGo to next message
amit1982
Messages: 1
Registered: December 2013
Location: Delhi
Junior Member
Hello Michel,
No output result of this querry, please help me to display the result.
my requirement is to display table name, row count, tablespace name, username, size for all schemas.
your help will be appreciated.
I tried from both sys user and user, but no error and no result.

Amit
Re: get row counts of tables in schema without using dba_tables.num_rows [message #604127 is a reply to message #604125] Tue, 24 December 2013 04:59 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

If I don't know what you did how could I know what was wrong?
Copy and paste your SQL*Plus session.

Previous Topic: Find no. of times column value modified
Next Topic: How to Create Packages and Procedures to automatically drop table indexes
Goto Forum:
  


Current Time: Thu Apr 25 20:34:15 CDT 2024