Get records count of all tables
Hi ,
I am trying to get the record count of all tables using dynamic query. I don't know how to put the value in placeholder. I tried the below code.
SET SERVEROUTPUT ON SIZE 1000000
DECLARE
CURSOR table_list
IS
select OBJECT_NAME from user_objects
where object_type in ('TABLE')
and object_name not like '%AUDIT_DDL%'
AND object_name not like 'MD_%'
AND object_name not like 'EXT_%'
AND object_name not like 'STG_%'
AND object_name not like 'SYS_%'
AND object_name not like 'TMP_%'
AND object_name not like 'TEMP_%'
order by 1;
v_count NUMBER :=0;
query_str VARCHAR2(1000);
BEGIN
FOR table_name IN table_list
LOOP
query_str :='SELECT COUNT(1) FROM ' || table_name.OBJECT_NAME;
dbms_output.put_line(query_str);
dbms_output.put_line('Table Name:' || table_name.OBJECT_NAME );
v_count:= execute immediate query_str;
dbms_output.put_line('Table Name:' || table_name.OBJECT_NAME || ', Count ' || v_count );
END LOOP;
END;
I know I am doing wrong in the bold lines. But not sure how to fix it. Please help. Thanks in advance.
Hi,
Welcome to the forum!
What you posted is basically right, assuming you really want to do dynamic SQL t all.
The only problem with
961618 wrote:
query_str :='SELECT COUNT(1) FROM ' || table_name.OBJECT_NAME; would be if the object name included special characters (such as single-quotes) or lower-case letters. To avoid any possible problems, I would put the object name inside double-quotes:
... query_str := 'SELECT COUNT (*) FROM "' || table_name.OBJECT_NAME
|| '"';
v_count:= execute immediate query_str;
The correct syntax is
execute immediate query_str INTO v_count;V_count will be the number of rows in a single table. Keep another variable (say total_v_count) that keeps the total count so far.
Do you really need dynamic SQL?
SELECT SUM (num_rows) AS total_rows
FROM user_tables
WHERE table_name NOT_LIKE '%AUDIT_DDL%
AND ...
;gets the same information, accurate as of the last time statistics were gathered, and some of the numbers may be approximate. Depending on how you use the results, that may be good enough for you. If you actually have 10,000,123 rows, and the query says you have 10,000,000, does it really matter?
Similar Messages
-
GETTING ROW COUNTS OF ALL TABLES AT A TIME
Is there any column in any Data dictionary table which gives the row counts for particular table..
My scenario is...i need to get row counts of some 100 tables in our database...
instead of doing select count(*) for each table....is there any way i can do it?
similary How to get column counts for each table..in database .For example
Employee table has 3 columns...empid,empname,deptno....i want count(empid),
count(empname),count(deptno) ...is there any easy way for finding all column counts of each table in data base? is it possible?Why does "select count(mgr) from emp" return null and not 13?Good question ;)
Seems that xml generation in principle can't handle »counting nulls«:
SQL> select xmltype(cursor(select null c from dual)) x from dual
X
<?xml version="1.0"?>
<ROWSET>
<ROW>
</ROW>
</ROWSET>
1 row selected.
SQL> select cursor(select count(null) c from dual) x from dual
Cur
0
1 row selected.
SQL> select xmltype(cursor(select count(null) c from dual)) x from dual
select xmltype(cursor(select count(null) c from dual)) x from dual
Error at line 1
ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00229: input source is empty
Error at line 0
ORA-06512: at "SYS.XMLTYPE", line 0
ORA-06512: at line 1
but
SQL> select xmltype(cursor(select count(1) c from dual)) x from dual
X
<?xml version="1.0"?>
<ROWSET>
<ROW>
<C>1</C>
</ROW>
</ROWSET>
1 row selected.Looks like a bug to me ... -
Trying to get row counts for all tables at a time
Hi,
i am trying to get row counts in database at a time with below query but i am getting error:
its giving me ora-19202 error..please advise me
select
table_name,
to_number(
extractvalue(
xmltype(dbms_xmlgen.getxml('select count(*) c from '||table_name))
,'/ROWSET/ROW/C')
count
from all_tables;ALL_TABLES returns tables/views current user has access to. These tables/views are owned not just by current user. However your code
dbms_xmlgen.getxml('select count(*) c from '||table_name)does not specify who the owner is. You need to change it to:
dbms_xmlgen.getxml('select count(*) c from '||owner || '.' || table_name)However, it still will not work. Why? As I said, ALL_TABLES returns tables/views current user has access to. Any type of access, not just SELECT. So if current user is, for example, granted nothing but UPDATE on some table the above select will fail. You would have to filter ALL_TABLES through ALL_SYS_PRIVS, ALL_ROLE_PRIVS, ALL_TAB_PRIVS and PUBLIC to get list of tables current user can select from. For example, code below does it for tables/views owned by current user and tables/views current user is explicitly granted SELECT:
select
t.owner,
t.table_name,
to_number(
extractvalue(
xmltype(dbms_xmlgen.getxml('select count(*) c from '||t.owner || '.' || t.table_name))
,'/ROWSET/ROW/C')
count
from all_tables t,user_tab_privs p
where t.owner = p.owner
and t.table_name = p.table_name
and privilege = 'SELECT'
union all
select
user,
t.table_name,
to_number(
extractvalue(
xmltype(dbms_xmlgen.getxml('select count(*) c from '||t.table_name))
,'/ROWSET/ROW/C')
count
from user_tables t
OWNER TABLE_NAME COUNT
SCOTT DEPT 4
U1 QAQA 0
U1 TBL 0
U1 EMP 1
SQL> SY. -
Query to display all records count of all tables in a schema
Hi,
I need to count all the records of all my tables in a certain schema and display the max amount of the record count. Do you have a script for this one??SQL> create function countrec(in_tab in varchar2) return number is
2 retval number;
3 begin
4 execute immediate 'select count(*) from '||in_tab into retval;
5 return retval;
6 end;
7 /
Function created.
SQL> select table_name, countrec(table_name)
2 from tabs;
TABLE_NAME COUNTREC(TABLE_NAME)
QUERY_TOOL_DATA_COLLECTION 5
TEST01 0
T1 14
EMP 14
SALGRADE 5
FILES 0
PROVA 0
TEST 0
T_MASTER 1
T_CHILD 3
TAB_ONE 30
TAB_TWO 10
A 3
B 4
C 3
D 3
BONUS 0
DEPT 4
18 rows selected.Max -
File to Jdbc: how to get record count from DB table
Hello,
i have a scenario file to JDBC....ihave to insert input file data to DB table.
now my requirement is ..if i insert a file with 50 records to db table on first time, and then next time if i insert the file with 100 records to db table....the count should start from 51 in db table.
if i insert another file 3rd time, with 25 records, the count shold start from 151...so how can i achieve this functionality....
i think following options:
1. in mapping write lookup to call JDBC sender channel and fetch count and map same to target filed of DB. if this is ok...please provide UDF code..2. db triggers (not suitable for my req.)
So kindly let meknow possible solutions and required UDF codes..please
Thanks in advance...SARAN>>>..if i insert a file with 50 records to db table on first time, and then next time if i insert the file with 100 records to db table....the count should start from 51 in db table.
if i insert another file 3rd time, with 25 records, the count shold start from 151...so how can i achieve this functionality....
i think following options:
Suggestions:
1)Create an id column in db table. Talk to DB guy to create oracle sequencer for that column. So, every time you insert record that field will be updated with oracle sequencer. You dont need to handle this.
Refer this link
http://www.techonthenet.com/oracle/sequences.php
2) If you want to handle via pi, following suggestions..
If you use PI 7.1 or above, use jdbc lookup in the mapping and do the query something like this
Refer this link
/people/jin.shin/blog/2008/02/15/sap-pi-71-mapping-enhancements-series-graphical-support-for-jdbc-and-rfc-lookups
Do select query as below
select count(*) from tablename;
the above query will return number of rows exist in the table. So use that value and map it in the target field.
If you use pi 7.0 or below then use the previous reply and do the UDF implementation for jdbc lookup during mapping. Because jdbc lookup does not support in those versions.
Hope that helps.
Baskar -
Hi Experts,
Does any one have an idea as to
how to get the record count of all DSOs in one shot?
Regards
DipaliTran ST14 allows you to run a background program that captures data on the 30 largest ODS. Might be able to run it, or use the program it runs as a starting point for a Z version. It also produces other lists 30 largest cubes, E anf F fact tables, dimension tables, PSA, master data, etc.
This alignment of the sample below isn't quite right, but it gives you an idea of the output:
Logical Name (BW) Active Data Active Data Activation Queue Activation Queue Rollback Rollback Changelog Changelog Changelog Total Size Growth RSDBEXFL LASTUSED TXTLG ODSOTYPE
Size (KB) records Size records Size records Name Size (KB) records (KB) (KB)
ZPU_O52 223084800 270319290 128 72020 0 0 /BIC/B0000357000 1898496 3020700 224983424 0 X 20080903190013 Line Items in Funds Management
ZSL_O52 164580224 285739820 128 215490 0 0 /BIC/B0000558000 3041280 5548000 167621632 0 X 20080903135047 Special Ledger Details
ZGL_O_02 94384640 253558550 128 394820 128 0 /BIC/B0000595000 2469888 5227867 96854784 0 X 20080903180839 General Ledger: Line Item Detail
PU_O32 72226624 114269900 128 13880 128 0 /BIC/B0000325000 1768448 3172567 73995328 0 0 FI Line Items in Funds Management (IS-PS)
ZARS_O70 65613632 131033900 128 0 128 0 /BIC/B0000537000 2744320 5117567 68358208 0 X 20080828130735 Activity-based Reporting
PU_O33 49556224 107794700 128 44533 0 0 /BIC/B0000327000 1468416 2495200 51024768 0 0 CO Line Items in Funds Management (IS-PS)
ZCBP_CON 7841792 11669800 128 13550 128 0 /BIC/B0000958000 9650176 15398900 17492224 0 X 20071114174148 COPY OF ZBBP_CON
ZAP_O_02 15391808 27049480 128 42380 0 0 /BIC/B0000594000 569344 817460 15961280 0 X 20080903190031 Accounts Payable - Vendors: Line Item Detail
ZCBBP_PO 6334464 9345900 128 11493 128 0 /BIC/B0000957000 9124864 14334000 15459584 0 X 20080818174802 Copy of 0BBP_PO
PU_O31 12421696 21020500 128 13123 0 0 /BIC/B0000310000 607232 1183300 13029056 0 0 Commitments Line Items in Funds Management (IS-PS)
ZAP_OTW1 10814208 28065630 128 1091100 128 0 /BIC/B0000608000 957440 2711567 11771904 0 X 20080903110025 Accounts Payable: Treasury Warrant Info
ZSEM_O51 6937344 17585900 128 0 128 0 /BIC/B0000584000 4678784 12101200 11616384 0 X 0 Complement Data for Costing
BBP_PO 991232 0 128 0 128 0 /BIC/B0000167000 128 0 10116480 0 0 Purchase Order - Single Documents
ZCBP_INV 3613696 5889633 128 7167 128 0 /BIC/B0000959000 4551680 6404367 8165632 0 X 20071114161251 COPY OF 0BBP_INV
ZPU_O51 7032896 14555300 128 2093 128 0 /BIC/B0000352000 160768 392960 7193920 0 X 20080903141737 Budget in Funds Management
ZSEM_O52 3749952 17608900 128 0 128 0 /BIC/B0000585000 2978944 10656100 6729152 0 X 0 Complement Cost Data from HRV1018
ZSLGS_O1 2781184 5666200 128 18360 128 0 /BIC/B0000841000 3241984 6964467 6023424 0 X 20080903145240 FISL: SRM Global Spend Detail
BBP_DS1 5011456 5870533 128 18070 128 0 /BIC/B0000198000 548864 846330 5560576 0 0 Actual Value for Purchase Order -Single Documents
ZIC_O03 4332096 9217400 128 10477 128 0 /BIC/B0000587000 45056 91627 4377408 0 0 ODS Material Stocks / Movements
BBP_SC 1542400 1493100 128 19663 128 0 /BIC/B0000733000 119808 382750 2363904 0 0 Shopping Cart - Individual Documents
ZRR_O01 290816 781850 128 1263 0 0 /BIC/B0001007000 1815552 4045433 2106496 0 X 20080211170935 Revenue & Refunds DSO
BBP_DOC 797184 1359533 128 6978 128 0 /BIC/B0000732000 481280 51307 1278720 0 0 Document Flow - Single Documents
BBP_CON 1227776 0 128 0 128 0 /BIC/B0000194000 128 0 1228160 0 0 Goods/Services Confirmation - Single Documents
ZAR_O_02 840960 1288300 128 1427 128 0 /BIC/B0000593000 132096 199870 973312 0 X 20080711125812 Accounts Receivable - Customers:Line Item Detail
SRPO_D1 311296 375290 128 6176 0 0 /BIC/B0001036000 384000 500900 695424 0 0 Purchase Order History
ZSLFM_O1 542720 2404400 0 0 0 0 /BIC/B0000666000 128 0 542848 0 0 Expenditure Estimation - FM T
BBP_INV 461824 0 128 0 128 0 /BIC/B0000196000 128 0 462208 0 0 Invoice - Single Documents
ZSL_O3 458752 2430400 0 0 0 0 /BIC/B0000670000 128 0 458880 0 0 Expenditure Estimation - SL Data T
ZPM_O51 196928 755080 128 0 128 0 /BIC/B0000894000 159744 703770 356928 0 X 20080507175910 Plant Material Valuation History
ZGL_O3 235520 2422767 0 0 0 0 /BIC/B0001041000 128 0 235648 0 0 Expend Est - GL Svc Dt T
There are DB dictionary views that contain this info as well, some of which is copied ot SAP tables. If you have Oracle DB, look for a table DBSTATTORA. -
Count(*) for all tables
Hi ,
I want the Query to get the table name and count(*) display in excel like this.Can i get count(*) from metadata table .Please let me know ??
ACCT 53
ACCT_CHEQUE 45
EMP 50
DEPT 90Karthick_Arp wrote:
A XML solution.
This one is not mine. This question comes up often in this forum. And once i saw this answer. And i thought its really cool so just saved it in my Google Note Book ;)You need to update your google note book. It doesn't take account of Index Organised Tables.
Based on answer from Laurent Schneider
http://laurentschneider.com/wordpress/2007/04/how-do-i-store-the-counts-of-all-tables.html
SQL> select
2 table_name,
3 to_number(
4 extractvalue(
5 xmltype(
6 dbms_xmlgen.getxml('select count(*) c from '||table_name))
7 ,'/ROWSET/ROW/C')) count
8 from user_tables
9 where iot_type != 'IOT_OVERFLOW';
TABLE_NAME COUNT
DEPT 4
EMP 14
BONUS 0
SALGRADE 5Edited by: BluShadow on Jul 8, 2009 12:00 PM -
Newbie ques : How to get the list of all tables in the database
Hi,
I'm very new to Oracle (using Oracle8i currently). I wanted to know if there is a way to get the list of all tables in the database. Like in mySQL you can use the command " show tables" to get the list of all the tables.
Any help will e greatly appreciated. Please "cc" any reply to [email protected] also.
thanks
DevenHi
Select table_name, owner from all_tables;
will give u all the tables in the database.
all_tables, dba_tables, user_tables
all_objects, dba_objects, dba_objects
there are many, more tables. login as system and query the tab and try to describe the tables.
Thanks
Malar -
How can I get record count with a query?
In Client/Server pplication,the client send a query to server.
How can server get record count by oracle call interface?
Is it need execute "select count(*) from ...."?Yes.
Either that or increment a counter for each record fetched and
loop round until you hit the last record.
The first method would be more efficient on large datasets. -
Table to get the list of all tables in the database
hi,
please let me knwo the table where i can get the list of all tables in the databasehi,
please let me knwo the table where i can get the list
of all tables in the databaseHi Michael,
Will you EVER start reading some documentation?
I guess it's not far that many regulars won't reply to those kind of questions.
Believe me, reading doesn't hurt (well, at least, most of the times).
Rgds,
Guido -
Is it possible to get a count of all the class files in a Flash Builder project?
Is it possible to get a count of all the class files in a Flash Builder project?
Thanks!Resultsets are one per connection unless you have copied them. Do you have lots of connections are you properly reusing them or at least closing and discarding all references?
See tutorial and Javadoc of API too -
Gettıng record number of all tables
how can get the record number(count(*) ) of all the tables of the specıfıc user.
u can query user_tables and see the num_rows column
of that table of a corresponding schema."U" can but you would you would not being doing it the best way if you do.
SQL> select table_name, num_rows from user_tables;
TABLE_NAME NUM_ROWS
EMP 15
TEST 2
SQL> select count(*) from test;
COUNT(*)
6
SQL>It only works if the stats have been gathered on the tables and you don't really want to be gathering stats every time you want to know how many rows there are on your tables.
The best and most accurate way to count the number of rows on tables is to use count(*). -
How to get the count of all the entries in the DB?
I want to know the way getting the count of the entries in a DB. Not using cursor to visit all entries and record the count.
Message was edited by:
user633842Hello,
Yes, depending on the type of database you are using, the
following statistics from DB->stat, look like what you want.
For Hash databases:
hash_ndata;
The number of key/data pairs in the database. I
For Btree databases:
bt_ndata;
For the Btree Access Method, the number of key/data pairs
in the database.
For the Recno Access Method, the number of records in
the database.
For Queue databases:
qs_ndata;
The number of records in the database.
Additional details are at:
http://www.oracle.com/technology/documentation/berkeley-db/db/api_c/db_stat.html
Thanks,
Sandra -
How to compare record count of two tables?
Hi,
i am in need of simple sql script where i can compare two table record count.
i have something like below:
(select count(*) from table1) minus (select count(*) from table2)
now the problem is if the table1 count greater then table2 count the output is fine.
If the table2 record count is more then i am getting zero as the output.
how can i get the difference in two tables record count?
Thanks a lot in advance.
--Raman.Doing a MINUS between the counts does not yield the diff.
e.g. if table A has 100 records and table B has 70 records then,
SELECT count(*) FROM A
minus
SELECT count(*) from B
will give 100 and not 30.
Try this:
SELECT (
CASE WHEN ((select count(*) cnt from A) - (select count(*) cnt from B)) <0
THEN ((select count(*) cnt from A) - (select count(*) cnt from B))* -1
ELSE ((select count(*) cnt from A) - (select count(*) cnt from B)) END) Difference
FROM dualor this is simpler
SELECT abs(((select count(*) cnt from A) - (select count(*) cnt from B))) difference FROM dualEdited by: Caitanya on Jan 9, 2009 7:12 AM
Applied abs function after seeing BluShadow's post :) -
How to get row count(*) for each table that matches a pattern
I have the following query that returns all tables that match a pattern (tablename_ and then 4 digits). I also want to return the row counts for these tables.
Currently a single column is returned: tablename. I want to add the column RowCount.
DECLARE @SQLCommand nvarchar(4000)
DECLARE @TableName varchar(128)
SET @TableName = 'ods_TTstat_master' --<<<<<< change this to a table name
SET @SQLCommand = 'SELECT [name] as zhistTables FROM dbo.sysobjects WHERE name like ''%' + @TableName + '%'' and objectproperty(id,N''IsUserTable'')=1 ORDER BY name DESC'
EXEC sp_executesql @SQLCommandThe like operator requires a string operand.
http://msdn.microsoft.com/en-us/library/ms179859.aspx
Example:
DECLARE @Like varchar(50) = '%frame%';
SELECT * FROM Production.Product WHERE Name like @Like;
-- (79 row(s) affected)
For variable use, apply dynamic SQL:
http://www.sqlusa.com/bestpractices/datetimeconversion/
Rows count all tables:
http://www.sqlusa.com/bestpractices2005/alltablesrowcount/
Kalman Toth Database & OLAP Architect
SQL Server 2014 Design & Programming
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012
Maybe you are looking for
-
Show/Hide Div Layer with CSS (k)
Is there a way you can control the visibility of a Div Layer without Javascript? Using CSS perhaps? Even though a small percentage doesn't have Javascript enabled, I need to have something to show. In this case a webpage that has button when clicked
-
Handling Session in jsp and bean scope
i have problems with handling session in jsp I want to check on all jsp pages first thing is session is null i want to him to be redirected to first page and he should not be able to go ahead I tried if(session ==null) response.sendRedirect("Login.js
-
How to use scb-68 board to send a trigger signal
I am using Labview 8.5. Our DAQ card is scb-68. I want to send a trigger signal out by using the DAQ. Could anybody tell me how to do that?
-
AQ Adapter failure - reinstall doens't help, unless a new name is provided
Hi Our AQ adapter worked fine for years, but after a restart it suddenly stopped working. No error messages are written when started but what is more strange, no log is written at all (the agent_log_level is set to 2, so it should write something). A
-
Trouble Exporting From FCP 5 to Motion
Hi, I'm new to this forum and have a question. I'm trying to export a sequence of clips from FCP to a motion project. The manual says File>Export>Export to Motion Project as a path but that option does not seem to exist. I've gone the "Send to Motion