How to count the number of rows in multiple Oracle Tables

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!
sjsimpson2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
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
PortletPaulfreelancerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sjsimpson2000Author Commented:
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

PortletPaulfreelancerCommented:
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
sjsimpson2000Author Commented:
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
PortletPaulfreelancerCommented:
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
PortletPaulfreelancerCommented:
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
sjsimpson2000Author Commented:
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
sjsimpson2000Author Commented:
Excellent help!
0
PortletPaulfreelancerCommented:
Good to hear its working. thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.