Solved

How to count the number of rows in multiple Oracle Tables

Posted on 2016-10-31
10
102 Views
Last Modified: 2016-11-02
Hi - I've been searching for a simple solution, but haven't found anything I can understand enough to make work.

I have 78 tables in my Oracle schema.  I need to create a select statement that will count the number of rows in each table, and display it in a format that can be exported to csv (two columns: one for the table name, the second for the number of rows).  I need to use count(*)  because the tables are not analyzed frequently (it is a test system).

I've seen several solutions out there, but my sql skills aren't very strong, and I've not been able to find something I can use.

Thanks for any help!
0
Comment
Question by:sjsimpson2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41867844
try 1 ..

--

SELECT table_name,   to_number(    extractvalue(       xmltype(          dbms_xmlgen.getxml('select count(*) c from ' || table_name))     ,'/ROWSET/ROW/C')) cnt from  user_tables;

--

Open in new window


try2

--

Select table_name as tableName , trim(column_value)  Cnts from user_tables, xmltable((
'count(ora:view("'||table_name||'"))'))

--

Open in new window


Hope it helps!
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 41867883
I just do it with something like this:

select
       'select ''' || table_name || ''' tname, count(*) c from ' || table_name || ' union all' gen_sql
from user_tables
where rownum < 11
order by table_name

Then copy that output, remove the final "union all" and run that sql.

e.g. http://rextester.com/KVNU42276

-- raw output from that source  (with their added line numbers)
1	select 'CUSTOMATTRIBUTES' tname, count(*) c from CUSTOMATTRIBUTES union all
2	select 'EMPLOYEEDATA' tname, count(*) c from EMPLOYEEDATA union all
3	select 'EMPLOYEEDATA1' tname, count(*) c from EMPLOYEEDATA1 union all
4	select 'EMP_TABLE' tname, count(*) c from EMP_TABLE union all
5	select 'HOTELAVAILABILITY' tname, count(*) c from HOTELAVAILABILITY union all
6	select 'PAVAN' tname, count(*) c from PAVAN union all
7	select 'SD' tname, count(*) c from SD union all
8	select 'SD9' tname, count(*) c from SD9 union all
9	select 'STD9' tname, count(*) c from STD9 union all
10	select 'T' tname, count(*) c from T union all

Open in new window

0
 

Author Comment

by:sjsimpson2000
ID: 41868397
Thanks for your replies.

Pawan - When I run your fist solution I get this error "ORA-19202: Error occurred in XML processing ORA-00933: SQL command not properly ended".  I'm running this in an APEX environment and get the same result in the SQL Commands tool and SQL Scripts tool.   tried several different versions of the output, but kept getting the error.

I get a "table or view does not exist" error when I try to run your solutions.  Since this is an APEX instance I'm running against, so I may not have access to xmltable.

PortletPaul - When I try your script I get either "ORA-00928: missing SELECT keyword" or "ORA-00923: FROM keyword not found where expected".  I tried several different versions of the output, but kept getting these errors.
0
Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

 
LVL 49

Expert Comment

by:PortletPaul
ID: 41869135
There are 2 steps to my suggestion. was it step 1 or 2 that failed?

IF step 2 can you paste a sample of what you executed please.

Note the url I provided shows a fully working query. you can test it there too.
0
 

Author Comment

by:sjsimpson2000
ID: 41869222
Hi PortletPaul, it was the 2nd step.  The first step created all the "select" statements.  I got the errors when I ran the 2nd step.  I'm doing this in a APEX environment, and sometimes that complicates things.  Let me try again...  Thx.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41869237
It wolud be helpful to see a few lines of the second step

Don't forget to remove the final "union all"

I can check in my Apex account later if needed. rarely use it I'll admit.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41869298
Running in https://apex.oracle.com environment, step 1 ran this:
select
       'select ''' || table_name || ''' tname, count(*) c from ' || table_name || ' union all' gen_sql
from user_tables
where rownum < 11
order by table_name

Open in new window

result:
select 'APEX$TEAM_DEV_FILES' tname, count(*) c from APEX$TEAM_DEV_FILES union all
select 'APEX$_ACL' tname, count(*) c from APEX$_ACL union all
select 'APEX$_WS_FILES' tname, count(*) c from APEX$_WS_FILES union all
select 'APEX$_WS_HISTORY' tname, count(*) c from APEX$_WS_HISTORY union all
select 'APEX$_WS_LINKS' tname, count(*) c from APEX$_WS_LINKS union all
select 'APEX$_WS_NOTES' tname, count(*) c from APEX$_WS_NOTES union all
select 'APEX$_WS_ROWS' tname, count(*) c from APEX$_WS_ROWS union all
select 'APEX$_WS_TAGS' tname, count(*) c from APEX$_WS_TAGS union all
select 'APEX$_WS_WEBPG_SECTIONS' tname, count(*) c from APEX$_WS_WEBPG_SECTIONS union all
select 'APEX$_WS_WEBPG_SECTION_HISTORY' tname, count(*) c from APEX$_WS_WEBPG_SECTION_HISTORY union all

Open in new window

copied that to sql window and pasted
Removed final "union all", this is what I executed:
select 'APEX$TEAM_DEV_FILES' tname, count(*) c from APEX$TEAM_DEV_FILES union all
select 'APEX$_ACL' tname, count(*) c from APEX$_ACL union all
select 'APEX$_WS_FILES' tname, count(*) c from APEX$_WS_FILES union all
select 'APEX$_WS_HISTORY' tname, count(*) c from APEX$_WS_HISTORY union all
select 'APEX$_WS_LINKS' tname, count(*) c from APEX$_WS_LINKS union all
select 'APEX$_WS_NOTES' tname, count(*) c from APEX$_WS_NOTES union all
select 'APEX$_WS_ROWS' tname, count(*) c from APEX$_WS_ROWS union all
select 'APEX$_WS_TAGS' tname, count(*) c from APEX$_WS_TAGS union all
select 'APEX$_WS_WEBPG_SECTIONS' tname, count(*) c from APEX$_WS_WEBPG_SECTIONS union all
select 'APEX$_WS_WEBPG_SECTION_HISTORY' tname, count(*) c from APEX$_WS_WEBPG_SECTION_HISTORY

Open in new window

THis was the result:
TNAME	C
APEX$TEAM_DEV_FILES	0
APEX$_ACL	0
APEX$_WS_FILES	0
APEX$_WS_HISTORY	0
APEX$_WS_LINKS	0
APEX$_WS_NOTES	0
APEX$_WS_ROWS	0
APEX$_WS_TAGS	0
APEX$_WS_WEBPG_SECTIONS	0
APEX$_WS_WEBPG_SECTION_HISTORY	0

Open in new window

0
 

Author Comment

by:sjsimpson2000
ID: 41870233
Tried again this morning, and it works as expected!  Not sure what I was doing wrong (one problem was that somehow tname got in front of the table name),  MANY THANKS for your help!!
0
 

Author Closing Comment

by:sjsimpson2000
ID: 41870237
Excellent help!
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41870274
Good to hear its working. thanks.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question