Rows in all tables in one query
Hi All,
How can i find the numbers of rows (records) in all the tables except system tables with single query.
In one tran table i have around 10,00,000 (TEN LACS) rows. while processing it take time. Plz suggest some solution for this.
Regards,
Mummy
Hi,
You could use the NUM_ROWS column in the ALL_TABLES table. This column only gets populated if you collect statistics on the relevant tables though.
See: http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2105.htm#REFRN20286
You can analyze the relevant tables first and then use a SUM. For instance this query gets the total number of rows in the tables in the schema MAST:
select sum(num_rows) from all_tables where owner = 'MAST';
Regards,
Marco
=
www.marcostuijvenberg.nl
Similar Messages
-
How can count no of rows in all tables in one schema
hi all
i want to cound no of rows in my schema ( all tables)
eg. i have 36 tables
i want to know no of rows in every tables in only one query through sql or plsql
how can i do..
regards
mohammadi
Message was edited by:
Mohdidubai52hi
thanx for ur reply
but i got error....
SQL> ED
Wrote file afiedt.buf
1 DECLARE
2 v_rowNo NUMBER := 0;
3 v_sum NUMBER := 0;
4 v_tableName VARCHAR2(100);
5 CURSOR c1 IS
6 SELECT table_name
7 FROM user_tables;
8 BEGIN
9 FOR counter IN c1 LOOP
10 DBMS_OUTPUT.PUT_LINE(counter.table_name);
11 EXECUTE IMMEDIATE 'SELECT COUNT(1) FROM ' || counter.table_name INTO v_ro
wNo;
12 v_sum := v_sum + v_rowNo;
13 END LOOP;
14 DBMS_OUTPUT.PUT_LINE('Number of rows: ' || v_sum);
15* END;
16 /
DECLARE
ERROR at line 1:
ORA-00933: SQL command not properly ended
ORA-06512: at line 11
again
thanx
regards
Mohammadi -
Count number rows in multiple tables from one query
Hi
I was wondering if its possible to have a single query return the number of lines in multiple tables, for example i have the tables
foo1
pk_foo1
and
foo2
pk_foo2
They are not joined together by any contraints. So the pseudo code for the query would be something like
SELECT numrows(pk_foo1), numrows(pk_foo2) FROM foo1, foo2
Thanks!without a join you get a cartesian product for the query:
SQL> select count(d.deptno),count(e.deptno)
2 from dept d,emp e
3 /
COUNT(D.DEPTNO) COUNT(E.DEPTNO)
105 105so you need to do a bit of trickery
1 select a.cnt,b.cnt
2 from
3 ( select count(d.deptno) cnt from dept d ) a,
4* ( select count(e.deptno) cnt from emp e) b
SQL> /
CNT CNT
7 15
SQL> select count(*) from dept;
COUNT(*)
7
SQL> select count(*) from emp;
COUNT(*)
15 -
Single-row subquery returns more than one query
Hi,
i'm receiving the following error: Single-row subquery returns more than one query. I have no idea how I should debug this query. For all I know, the subqueries can only return 1 row... :S
I only have Oracle SQL Developer to run/test my queries. I can run the query with ROWNUM < 5000, which gives no errors. If I run with ROWNUM > 5000 and ROWNUM < 6000 then the error appears....
Here's the query:
with
DATES as
(select (select min(REC_DATE) from STOCK) as FROM_DT, (select max(CHANGE_DATE) from STOCK_ADJUST) as TO_DT from DUAL),
MONTHS as
(select add_months(trunc(FROM_DT,'MM'),ROWNUM-1) as DT from DATES connect by ROWNUM <= months_between(TO_DT, FROM_DT)+1),
CALCULATIONS as
(select
PNM.PNM_AUTO_KEY PNM_KEY,
MONTHS.DT DT,
NVL((select sum(stm.qty_rec) from stock stm WHERE STM.REC_DATE < MONTHS.DT AND STM.PNM_AUTO_KEY = PNM.PNM_AUTO_KEY GROUP BY pnm_auto_key),0) INCOMING,
NVL((select sum(saj.qty_adj) from stock_adjust saj
inner join stock stm on saj.stm_auto_key = stm.stm_auto_key
inner join parts_master pnm on stm.pnm_auto_key = pnm.pnm_auto_key where SAJ.CHANGE_DATE < MONTHS.DT AND STM.PNM_AUTO_KEY = PNM.PNM_AUTO_KEY group by pnm_auto_key),0) OUTGOING
from MONTHS, PARTS_MASTER PNM)
SELECT
ROWNUM, CALCULATIONS.PNM_KEY PNM_AUTO_KEY, CALCULATIONS.DT COUNT_DATE,CALCULATIONS.INCOMING QTY_RECEIVED, CALCULATIONS.OUTGOING QTY_USED, (CALCULATIONS.INCOMING + CALCULATIONS.OUTGOING) QTY_BALANCE
FROM
CALCULATIONS;Edited by: user574699 on Nov 17, 2008 2:10 AM code formattingI suppose (how can i be sure?) that the queries in question are:
NVL((select sum(stm.qty_rec) from stock stm WHERE STM.REC_DATE < MONTHS.DT AND STM.PNM_AUTO_KEY = PNM.PNM_AUTO_KEY GROUP BY pnm_auto_key),0) INCOMINGand
NVL((select sum(saj.qty_adj) from stock_adjust saj
inner join stock stm on saj.stm_auto_key = stm.stm_auto_key
inner join parts_master pnm on stm.pnm_auto_key = pnm.pnm_auto_key where SAJ.CHANGE_DATE < MONTHS.DT AND STM.PNM_AUTO_KEY = PNM.PNM_AUTO_KEY group by pnm_auto_key),0) OUTGOINGBoth queries have an aggregate function based on the grouping on PNM_AUTO_KEY. Both WHERE clauses also include the = PNM_AUTO_KEY statement. So how can these return more than 1 row?
As I stated I cannot run the subqueries without the rest. The database contains over 200000 PNM_AUTO_KEYS, and I don't know which one is causing the problems.... -
Prevent user from deleting rows from all tables in his own schema
Hi,
How can I prevent user from deleting rows in all tables in his own schema.
I want the user to not able to delete rows from any existing or new tables that might be added in the future.
The user does not have the "DELETE ANY TABLE" system privilege.
Please advise.
Thanks.Nowadays, I'd also avoid triggers (if possible).
Sometimes, when I daydream, I'm rewriting a few applications that I've contributed to as a newbie, and I'm very ashamed of it nowadays.
From what I've experienced, in retrospective, the emphasis on teaching 'Oracle stuff' has been lying far too much on PL/SQL row-by-row oriented processing instead of letting Oracle 'crunch' sets at once.
Most of my debugging hours ended up in discovering one or more database triggers 'doing stuff automagically'.
Another nice blogpost: http://rwijk.blogspot.com/2007/09/database-triggers-are-evil.html
Regarding OP's question:
I would just rethink/reconsider this requirement completely.
Correctly implementing privileges and roles seems the best way to go, yes.
Triggers? Nah...
pre-post-edit, noticed thread got updated just before posting
Don't know what you mean with 'namedropping', but I think it's legitimate to point other readers to interesting Oracle related opinions/articles that do have a technical background and lots of interesting examples.
post dreaded OTN outage edit (from here)
Again: I would just rethink/reconsider this requirement completely.
Both trigger/vpd are being used to hide a design flaw here. -
What is the problem with native dynamic sql when counting rows in all table
what is the problem with native dynamic sql when counting rows in all table?Giving an error "table or view does not exist". Thanks.
DECLARE
v_sql_string varchar2(1000);
v_no_of_rows number;
BEGIN
for i in ( select table_name from all_tables )
loop
v_sql_string := ' select count(1) from ' || i.table_name;
dbms_output.put_line( v_sql_string );
--execute immediate v_sql_string into v_no_of_rows;
end loop;
END;Usually your problem can be described with 'Who cares'. I mean, for what reason do you do this? I doubt that there's a business need to get 100 % accurate answers for this. Normally such things are used to get a picture about the growth of data.
Personally I would prefer to have up-to-date statistics for all tables and just query the number of rows from there. Sufficient for me in < 99 % of all cases.
Just my $ .02... -
How to delete all data in all tables in one time ?!
1 CREATE OR REPLACE PROCEDURE delete_all_data
2 IS
3 v_statement VARCHAR2 (200);
4 BEGIN
5 FOR i IN (SELECT *
6 FROM user_tables)
7 LOOP
8 v_statement :=
9 'delete table ' || i.table_name ;
10 EXECUTE IMMEDIATE v_statement;
11 END LOOP;
12 commit;
13* END;
SQL> /
Procedure created.
SQL> exec delete_all_data
BEGIN delete_all_data; END;
ERROR at line 1:
ORA-00903: invalid table name
ORA-06512: at "DE2.DELETE_ALL_DATA", line 10
ORA-06512: at line 1
I made the previous code , but it's didn't work with me .... any help for this problem please ?!
I'm just reminder ..... all what I need ,that delete all data in all tables in one time only .
I'm waiting for the answer ..... and thanks in advancecreate or replace
FUNCTION TRUNC_SCHEMA RETURN NUMBER AS
CURSOR select_table IS SELECT TABLE_NAME AS TNAME FROM USER_TABLES ORDER BY 1;
sTableName Varchar2(128);
sUser Varchar2(128);
sConstraintName Varchar2(128);
plsql_block Varchar2(512);
BEGIN
SELECT USER INTO sUser FROM DUAL;
IF ((sUser='SYSTEM') OR (sUser='SYS')) THEN
RETURN 1;
END IF;
EXECUTE IMMEDIATE 'PURGE RECYCLEBIN';
-- DISABLE table's constraints
FOR C1 IN (select CONSTRAINT_NAME, TABLE_NAME from user_constraints where STATUS = 'ENABLED' AND CONSTRAINT_TYPE in ('P','R') ORDER BY R_CONSTRAINT_NAME) LOOP
sConstraintName := C1.CONSTRAINT_NAME;
sTableName := C1.TABLE_NAME;
plsql_block := 'ALTER TABLE ' || sTableName || ' DISABLE CONSTRAINT ' || sConstraintName;
EXECUTE IMMEDIATE plsql_block ;
END LOOP;
FOR D IN select_table LOOP
--get table name
sTableName := D.TNAME;
-- clear table
plsql_block := 'TRUNCATE TABLE ' || sTableName;
EXECUTE IMMEDIATE plsql_block ;
END LOOP;
-- ENABLE table's constraints
FOR C2 IN (select CONSTRAINT_NAME, TABLE_NAME from user_constraints where STATUS = 'DISABLED' AND CONSTRAINT_TYPE in ('P','R') ORDER BY R_CONSTRAINT_NAME desc) LOOP
sConstraintName := C2.CONSTRAINT_NAME;
sTableName := C2.TABLE_NAME;
plsql_block := 'ALTER TABLE ' || sTableName || ' ENABLE CONSTRAINT ' || sConstraintName;
EXECUTE IMMEDIATE plsql_block ;
END LOOP;
RETURN 0;
EXCEPTION WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERROR: ' || SQLERRM);
RETURN 1;
END TRUNC_SCHEMA;
/ -
Question about import all tables except one
I have exported all tables for a user. While importing I want to import all tables except one table whose name I know. How do I do this? Example: If I have tables: Employee, address, salary that I have exported. While importing I want to import only Employee and address. How do I do this. The database I have exported contains over 20 tables.
I don't think it is possible at all, you just need to explicitly specify all the tables.
If you really don't want to include the single table, then you can import all the tables and drop the desired one.
hare krishna
Alok -
Need Query to that generate count of rows of all tables
Hi
i need a query which gives the result of no.of rows in talbe and coresponding table name.
And then i need to compare the same with other DB schema
Thanks in advanceHi User,
We can also get the count of rows for all the tables associated with a User, we can create a custom function which
uses the table name to return results.
CREATE OR REPLACE FUNCTION TAB_ROWS_CNT (TAB_NAME IN VARCHAR2)
RETURN NUMBER
AS
TAB_CNT NUMBER :=0;
BEGIN
EXECUTE IMMEDIATE 'select count(*) from ' || TAB_NAME
INTO TAB_CNT;
RETURN TAB_CNT;
END;And query,
SELECT TABLE_NAME, TAB_ROWS_CNT (TABLE_NAME) ROW_CNT
FROM USER_TABLES;Which gets us the count of Individual Tables for a user.
This is an just that we can do in this way also. But, use which is optimal.
Thanks,
Shankar -
Hi,
I need to query in max db to find number of rows exist in each and every table. Is there any query we can use to find in a single shot?
Example:
S.NO Table Name Number of rows
1 A 3000
2 B 5000Hi,
it depends on the database version you are using. In the newer database versions 7.8 and 7.9 you can use the system table files to get the total number of records.
SELECT sum (entrycount) from files where type = 'TABLE'
This includes the MaxDb systemtables as well.
If you want to count the number of rows of one schema only use the following join command between tables and files:
SELECT sum(entrycount) from files f, tables t where f.type = 'TABLE'
AND tableid = fileid and SCHEMANAME = '<schema>'
e.g.
SELECT sum(entrycount) from files f, tables t where f.type = 'TABLE'
AND tableid = fileid and SCHEMANAME = 'SUPERU'
The system table files contains for each table the entrycount, which is exaclty the number of records.
Precondition: All file directory counters for all tables have been created sucessfully. This is the case if table Sysupdatecounterwanted is empty. The creation is implictely executed.
Regards, Christiane -
SQL Server 2014 - Delete duplicate rows in a table except one
Hi all,
I have a table that contains duplicate rows. Using grouping I can easily identify those rows.
As of now, I count the grouped records and copy one of the group in a temporary table together with its count of elements. Than I loop over this table and delete N - 1 rows of each group from the original table. Unfortunately this is done with two cursors
and WHILE loops.
Is anybody out there who could give some hints how to handle deletion of duplicated rows leaving one in place using pure SQL? I tried a full delete of all elements of a group and then an INSERT of one row to recreate a row again. But this is not
very elegant and close to using cursors.
Regards UweHi all,
based on input below from Uri, I have developed a little more general solution for removal of duplicates. I did that, because I'm sure that for many beginners in SQL it is easier specialize a proposal by removing parts of it than adding new functionality.
Here is my code.
-- Remove duplicates from a table and document the deleted rows
DECLARE @MyTable Table (Col1 int, Col2 int, Col3 int, Col4 int);
DECLARE @OutTable Table (Col1 int, Col2 int, Col3 int, Col4 int);
INSERT INTO @MyTable VALUES
(1, 10, 100, 7),
(2, 20, 200, 2),
(2, 20, 200, 1),
(3, 30, 300, 3),
(1, 10, 200, 4),
(1, 10, 100, 5),
(1, 10, 100, 6);
WITH cte (RN, Col1, Col2, Col3, Col4)
AS (SELECT ROW_NUMBER() OVER (PARTITION BY Col1, Col2, Col3 ORDER BY (SELECT 0)) AS RN, Col1, Col2, Col3, Col4
FROM @MyTable)
DELETE FROM cte
OUTPUT DELETED.Col1, DELETED.Col2, DELETED.Col3, DELETED.Col4
INTO @OutTable (Col1, Col2, Col3, Col4)
WHERE RN > 1;
SELECT * FROM @OutTable;
And this is the result:
The MS 139444 article Satish mentioned is focussed on duplicate PKs removal. Thanks for the hint.
Thanks to you all for your rapid response.
Regards Uwe -
EJB QL - removal from 2 tables in one query
Hi All,
I have a question - can I delete data from 2 tables in the one EJB 3 query ?
For example:
delete from AaaaaSaaaaRaaaaa a, AaaaaFaaaaaRaaaaa b where a.id = b.id and a.startTime < :STARTTIMEAaaaaSaaaaRaaaaa and AaaaaFaaaaaRaaaaa - two separated entities mapped on the different tables.
In this case I got exception:
ERROR [PARSER] line 1:76: unexpected token: ,Thanks !I think .. answer is - NO :(
Another question - how to delete data from 2 tables in the one query in some other way (with condition above and with EJB QL)?
Thanks -
Update SAME column name in two tables from ONE query
Dear All Seniors
Please tell me is it possible to update a same column name in two tables.
I have two tables in same schema
(1)table name
pem.igp_parent
column name
igp_no.
igp_type
(2)table name
pem.igp_child
column name
igp_no.
igp_type
i want to update igp_no column in one query please tell me how it would be possible.
thanks
yassenYou want to update the data from what to what? Where is the new data coming from?
If you are trying to put the same data in two different tables, that strongly implies that you have a normalization problem that needs to be addressed.
Why do you want a single query rather than updating each table in turn? Can you join the two target tables to produce a key-preserved view?
Justin -
Merge Two Rows of a table to One row but into two columns
Hi
I Am struck in writing a query to merge two rows into two columns of one row.
Here is the Sample data i am working with,
Col 1 Col 2 Col3 Col4 Col Col6
5000 573-3000 2 0 Phone
5000 573-3036 1 0 Fax
5000 893-5703 3 0 WOrk
3000 232-5656 1 0 Phone
3000 353-5656 2 0 FAx
Here Col,Col3,Col4 form the Key.
now wht i am trying to do is to Merge these type of rows put them into Columns P,F,W,E respectively to achive a Structure as below
Col1 P F W
5000 573-3000 573-3036 893-5703
3000 232-5656 353-5656
Can you please help me how could i do this.
I am pretty ordinary at writing SQL's.
Thanks a Lot in Advance
Message was edited by:
SreebhushanSearch the forum for PIVOT and you'll find plenty of examples.
-
Is there any easy way to count all of the rows in all of the tables?
I am currently generating a list of tables by selecting from all_tables and then turning that into individual select count(*) statements using awk.
I'm guessing there is probably some other better method?Jim,
I have a sql script which I call numrows that will dynamically create select count(*) statements from the dba_tables dictionary view... It does not display all of the tables in the entire database, but instead prompts you for a particular schema. Maybe you can adjust to do what you want it to..
Regards,
David
+++beginning of script
-- Script to count the number of rows in tables
set serveroutput on
declare
numrows integer;
cursor c1 is select table_name from user_tables order by table_name;
function rowcount(tablename in user_tables.table_name%type)
return integer is
cursornum integer;
numrows integer;
ignore integer;
begin
cursornum := dbms_sql.open_cursor;
dbms_sql.parse(cursornum,
'select count(*) from ' | | tablename,
dbms_sql.v7);
dbms_sql.define_column(cursornum, 1, numrows);
ignore := dbms_sql.execute(cursornum);
ignore := dbms_sql.fetch_rows(cursornum);
dbms_sql.column_value(cursornum, 1, numrows);
dbms_sql.close_cursor(cursornum);
return numrows;
end;
begin
dbms_output.enable(10000);
dbms_output.put_line('Table Rows ');
dbms_output.put_line('------------------------------ ----------');
for c1rec in c1 loop
numrows := rowcount(c1rec.table_name);
dbms_output.put_line(rpad(c1rec.table_name, 32) | | numrows);
end loop;
end;
null
Maybe you are looking for
-
Number to a binary base number
How can I convert a numeric data to a binary base number? Ex.: A into 0001010 Thank you in advance
-
iTunes 11.1.3 still allows you to add PDF books to library and the imported books show up in the albums view under the music selection. Seems weird, as books tab was removed from itunes. Importing 10 books results in creation of an album tab which is
-
Journal Voucher PLD, Reversal Date
Dear Experts, At Journal Voucher PLD, choose Source Type = Database, Table = Journal Voucher Entry, Column = Reversal Date. But the printout is blank. Check database OBTF, StornoDate got value there, may i know why it is not printed? The SAP Version
-
How do you get around keynote crashing when trying to open up a large PC powerpoint file?
Please Help?
-
I'm running Yosemite beta 3 on a MBP late 2011. Everytime I restart my machine Skype crashes. I have to reinstall it to get it working again. Can anyone help me? Attachments: Skype_2014-07-14-142832_Xaviers-MacBook-Pro.crash 70 KB