Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to count the number of rows in multiple Oracle Tables

Posted on 2016-10-31
10
Medium Priority
?
119 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 35

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 2000 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 and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

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ā€¦
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.
Suggested Courses

824 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