0

I want to count number of rows for all tables in user-defined schemas. I have got solution related to specific schema counts. But I want to get the following output

 TABLE_NAME   SCHEMA_NAME   ROW_COUNT

The main concern is ALL USER-DEFINED SCHEMAS ONLY. I can't figure out that how to identify that whether it's user schema or system schema. Moreover, when I execute following script

declare
    v_count integer;
begin

    for r in (select table_name, owner from all_tables where owner not in ('SYS','SYSTEM','OUTLN','DBSNMP','APPQOSSYS','WMSYS','CTXSYS','XDB' ) )
    loop
        execute immediate 'select count(*) from ' || r.table_name 
            into v_count;


            DBMS_OUTPUT.PUT_LINE(r.table_name ||v_Count);
        INSERT INTO STATS_TABLE(TABLE_NAME,SCHEMA_NAME,RECORD_COUNT)
        VALUES (r.table_name,r.owner,v_count);
    end loop;

end;

But it returned following error

Error report - ORA-00942: table or view does not exist ORA-06512: at line 7 00942. 00000 - "table or view does not exist" *Cause:
*Action:

Thanks in advance for valuable responses.

Muhammad Waheed
  • 1,048
  • 1
  • 13
  • 30
  • When you get to 12c you can use the ORACLE_MAINTAINED column in DBA_USERS. Before that, the USER_ID might give a clue assuming system users were created prior to normal users and so should have a lower number (though upgrades might affect that so check carefully). – William Robertson Apr 22 '19 at 08:52

2 Answers2

1

Just replace all_tables with user_tables, since you can only query the tables of your current schema without a schema prefix. So, The problem raises due to such tables which your schema doesn't own.

Edit ( after the comment ) : If you want to get all the table's count through the database, you need to log on as a super user such as SYS, SYSTEM, and convert your SQL string to

'select count(*) from '|| r.owner||'.'||r.table_name

and run your script again.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
1

change your execute immediate statement as below it should work

  execute immediate 'select count(*) from '||r.owner||'.'|| r.table_name 
            into v_count;
Tamil
  • 141
  • 2