Solved

How to count the number of rows in multiple Oracle Tables

Posted on 2016-10-31
10
68 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 28

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 48

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
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
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 48

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 48

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 48

Expert Comment

by:PortletPaul
ID: 41870274
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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
File generation using utl_file 4 55
report returning null 21 96
Distinct values from all columns in a table?? PL SQL 4 38
Select and Insert Query running slow 4 36
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

777 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