Solved

How to count the number of rows in multiple Oracle Tables

Posted on 2016-10-31
10
43 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
  • 5
  • 4
10 Comments
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
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
Comment Utility
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
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
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
Comment Utility
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
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 48

Expert Comment

by:PortletPaul
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent help!
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
Good to hear its working. thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now