Count Rows in cursor
How I can count number of rows in a cursor?. In other words, how I can count how many records set in one cursor
<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by quang dau ([email protected]):
How I can count number of rows in a cursor?. In other words, how I can count how many records set in one cursor <HR></BLOCKQUOTE>
Hi
You can use c1%rowcount where c1 is name of cursor.
good luck
Kavita
null
Similar Messages
-
How to get the total count of the no. or rows a cursor has fetched
Hi All,
Is there a way to get the number of rows a cursor query has fetched, without looping into the cursor and using a counter variable
I mean like in pl/sql table we can directly use the pl/sql table attribute .count is there a way to achieve this in cursor
thanks a lot in advanceQwerty wrote:
Is there a way to get the number of rows a cursor query has fetched, without looping into the cursor and using a counter variableYes.
It is zero.
On the first loop and first fetch it will be 1, second 2 etc.
This is because cursors have no rows, or fetch no rows until your code or the client uses the cursor to fetch them.
I mean like in pl/sql table we can directly use the pl/sql table attributeA PL/SQL array is not like a cursor, a PL/SQL array has the rows already fetched into it.
A cursor is not an array but a pointer to a compiled SQL statement so you cannot count how many rows it has, because it has none. You can only count how many rows you fetch from it. -
Count rows from multiple tables using SQL only
Hi, I know this has probably been answered before, but I couldn't find the answer anywhere. Please help.
I'd like count(*) [rows] for all tables in database using SQL only - no PL/SQL
The result should be something like:
Table RowCount
DBA_TABLES 1000
DBA_USERS 50
etc.
Thanks!offcource write this script:
create or replace procedure count_tables (ip_schema VARCHAR2)
is
lv_owner VARCHAR2(100);
lv_table_name VARCHAR2(100);
lv_sql_statement VARCHAR2(2000);
lv_count_table NUMBER;
CURSOR c1 IS
SELECT owner, table_name
FROM all_tables
WHERE owner = ip_schema
ORDER BY table_name;
begin
dbms_output.put_line ('+--------------------------------------------------------------------+');
dbms_output.put_line ('¦ | | ¦');
dbms_output.put_line ('¦ Schema Name | Table Name | Number of Rows ¦');
dbms_output.put_line ('¦ | | ¦');
dbms_output.put_line ('¦------------------------------------------------------------------¦');
OPEN c1;
LOOP
FETCH c1 INTO lv_owner , lv_table_name;
EXIT WHEN c1%NOTFOUND;
lv_sql_statement := 'SELECT count(*) FROM ' || lv_owner || '.' || lv_table_name;
EXECUTE IMMEDIATE lv_sql_statement INTO lv_count_table;
IF lv_count_table > 0 THEN
dbms_output.put_line ('| '||rpad(lv_owner, 14, ' ')||'| '|| rpad(lv_table_name, 32, ' ')||'| '|| rpad(lv_count_table, 16, ' ')||' |');
-- dbms_output.put_line ('|---------------|---------------------------------|------------------|');
END IF;
END LOOP;
CLOSE c1;
dbms_output.put_line ('+--------------------------------------------------------------------+');
exception
WHEN OTHERS THEN
dbms_output.put_line ('owner: '||lv_owner||' - table: '||lv_table_name||' - '||sqlerrm);
end count_tables;
set serveroutput on size 1000000
exec count_tables
drop procedure count_tables; -
Count rows from several tables
hello,
im trying to count row from multiple tables
for example i need the select statement to produce the following
table_name count
table1 5
table2 6
table3 3
i came up with the following script but it counts the number of tables i have
select object_name, (select count(*) from user_tables where table_name = object_name) from all_objects
where object_type = 'TABLE'Manik wrote:
May be possible:
Check this:
SELECT table_name,
TO_NUMBER (
EXTRACTVALUE (
xmltype (
DBMS_XMLGEN.getxml ('select count(*) c from ' || table_name)),
'/ROWSET/ROW/C'))
COUNT
FROM (select * from all_tables where table_name in ('TABLE1','TABLE2'))
WHERE owner = 'SCOTT';Cheers,
Manik.Awesome Manik... Just too good. Thanks.
I wish i could have given you the 'Correct' points. ;-)
Can you please explain the logic in brief? Will be helpful for everybody to understand... -
SQL Developer 2.1 EA - count rows with filter on a table
This is a small problem that I came across today while using 2.1 EA. I opened a table and did a filter to narrow down the results of the table. Then when I right clicked on the search results and clicked on 'Count Rows'....I get the count of the rows in the table and not my search results.
Is this a feature or a bug?
Thanks,
MikeIn EA2, we get both the total and the filtered total, nice.
Only remark is we don't get a progress indicator anymore, and thus can't cancel a query that might take up hours to finish!
Please log a bug for this.
Thanks,
K. -
Count rows in an internal table
Anybody how knows how I can count rows in my internal table?
Hi,
Data: lines like sy-tabix.
DESCRIBE TABLE ITAB LINES LINES.
where itab is your internal table.
This will work out.
Please award sutiable points .
Regards,
Irfan -
Displaying count(*) in the cursor record
Hi,
I have the following cursor:
CURSOR inputs_cur IS
SELECT a, b, COUNT(*) cnt FROM INPUTS
GROUP BY a, b
ORDER BY COUNT(*) DESC;
inputs_rec inputs_cur%ROWTYPE;
BEGIN
OPEN inputs_cur;
LOOP
FETCH inputs_cur INTO inputs_rec;.
How can I reference count(*) in the cursor record?
d_str := inputs_rec.a || ' ' || inputs_rec.b || inputs_rec.cnt ( or cnt) ???
inputs_rec.cnt ( or cnt) does not display any data....
Thanks
IaSQL> Declare
2 Cursor C1 is Select a.c1 col1,count(*) col2 from
3 (Select 1 c1 from dual connect by rownum<=10
4 union all
5 Select 2 c1 from dual connect by rownum<=20) a
6 Group by a.c1;
7 abc c1%rowtype;
8 begin
9 Open C1;
10 loop
11 Fetch C1 into abc;
12 exit when C1%NOTFOUND;
13 dbms_output.put_line('Record Value: ' || abc.col1);
14 dbms_output.put_line('Count of record: ' || abc.col2);
15 end loop;
16 end;
17 /
Record Value: 1
Count of record: 10
Record Value: 2
Count of record: 20
PL/SQL procedure successfully completed.
SQL> -
How to set Tile count column or count row?
How to set Tile count column or count row? If not can do, How
to adjust count row or count column?
Thx for all idea.The Tile container's number of columns and rows is calculated
based on each child's width and height (or based on tileWidth and
tileHeight which you can set). If you need more control over the
layout, use Grid. -
Can i count row from ResultSet ?
I would like to count row from ResultSet for
take it into my array object because i must know number of row before create array object.
Example:
ResultSet rset = stmt.executeQuery("select * from user ");
/*i = amount of ResultSet*/
User[] user = new User;
int l=0;
while (rset.next()){
user[l] = new User();
user.name = rset.getString(1);
l++;Hi,
As per my knowledge there is no method by which you can get the count of items in a resultset directly. You will have to loop through the reseltset and set a variable for count. In your specific case I would advise you to use a Vetor instead of an array so that you need not bother about the size.
ResultSet rset = stmt.executeQuery("select * from user ");
Vector user = new Vector();
while (rset.next()){
user.addElement(rset.getString(1));
Now you will have a Vector that holds the user info. To retrieve the user info loop through the Vector.
for (int i; i<user.size(); i++){
userName = user.elementAt(i);
Hope I was of some help.
cheers!!!
Nish -
Count rows that are max timestamp
Hi,
I'm trying to run a query that will count only rows that are max timestamp.
i have a submit table which has dates of submissions, however some application numbers have multiple rows in this table. I need to count only one row per application and it has to be the latest submission data.
I have tried a subquery and its not working, I'm stuck at this point.
Thanks for help :)
example table
pk app # submit_date
12 test-1 02222011 13:30
13 test-2 02232011 09:45
14 test-1 02232011 09:51
how do i count rows but based on max timestamp?select
count(s.pk)
from
submit s
where exists (select s1.app#, max(s1.submit_date) from submit s1 group by s1.app#)
I dont really understand what you are doing there.
SQL> WITH T
2 AS (SELECT 12 pk, 'TEST-1' app#, SYSDATE submit_date FROM DUAL
3 UNION ALL
4 SELECT 13 pk, 'TEST-2' app#, SYSDATE + 1 submit_date FROM DUAL
5 UNION ALL
6 SELECT 14 pk, 'TEST-1' app#, SYSDATE + 2 submit_date FROM DUAL)
7 SELECT * FROM T;
PK APP# SUBMIT_DA
12 TEST-1 23-FEB-11
13 TEST-2 24-FEB-11
14 TEST-1 25-FEB-11
SQL> WITH T
2 AS (SELECT 12 pk, 'TEST-1' app#, SYSDATE submit_date FROM DUAL
3 UNION ALL
4 SELECT 13 pk, 'TEST-2' app#, SYSDATE + 1 submit_date FROM DUAL
5 UNION ALL
6 SELECT 14 pk, 'TEST-1' app#, SYSDATE + 2 submit_date FROM DUAL)
7 SELECT pk, app#
8 FROM (SELECT pk, app#, ROW_NUMBER () OVER (PARTITION BY app# ORDER BY submit_date DESC) rn
FROM T)
9 WHERE rn = 1;
PK APP#
14 TEST-1
13 TEST-2
SQL> -
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... -
Keeping track of count of open cursors
Is there a way to ask the API for a list of open cursors (or at least a count of) ?
Hi Linda,
Again, thanks for the reply.
Yes, I wanted to use it to manage cursors while closing a database. At the moment I maintain a count of open cursors myself, I do this so that I can manage the close database process.
I do this if I want to close the database:
1. Prevent new cursors from being opened (reject all db updates & queries)
2. Wait till open cursor count is zero
3. close db
However, it does mean having to wrap every query/update method in such a way that I can maintain the cursor count. I was setting up a few new databases and was just wondering if i'd missed something in the API to do this for me.
Thanks,
Joel
Edited by: JoelH on 17-Dec-2009 08:57 -
I believe this might have been asked multilpe times, but I could not find anything.
Is there any way to get the number of rows in a cursor, when I open the cursor. In my case, I have to do the processing in 3 different ways depending on whether the cursor has A)no rows, B) One row, c) more than one row. I know I can get the number of rows by fetching all the rows and then getting the %rowcount.
I wanted to know whether its possible before fetching any row. The ways I am currently doing it is as follows
blah blah blah is
Cursor cur = <my cursor query>;
cont number;
BEGIN
select count(*) into cont from <things exactly same as my cursor query>
if (cont = 0) THEN
<Processing A>
ELSIF (cont = 1) THEN
<Processing B>
ELSE
<Processing C>
END IF;
END
Is this method better than getting the the cursor, fetching at least 2 rows, if the rowcount is more than one, close the cursor and do the processing based on the rowcount i've got.
TIAcreate or replace
package Y1 is
procedure do_it (p_1 number) ;
end Y1;
create or replace
package body Y1 is
procedure do_it (p_1 number) is
cursor c1_cur (p_1 number) is
select tname from tab where rownum <= p_1
c1_rec c1_cur%rowtype ;
c2_rec c1_cur%rowtype ;
l_count number ;
begin
l_count := 0 ;
open c1_cur(p_1) ;
LOOP
if l_count = 0 then
fetch c1_cur into c1_rec ;
else
fetch c1_cur into c2_rec ;
end if ;
exit when c1_cur%notfound ;
l_count := l_count + 1 ;
if l_count = 2 then
dbms_output.put_line ('XXX=2') ;
dbms_output.put_line (c1_rec.tname) ;
dbms_output.put_line (c2_rec.tname) ;
elsif l_count > 2 then
dbms_output.put_line ('XXX>2') ;
dbms_output.put_line (c2_rec.tname) ;
end if ;
END LOOP ;
close c1_cur ;
if l_count = 0 then
dbms_output.put_line ('XXX=0') ;
dbms_output.put_line ('===========================') ;
elsif l_count = 1 then
dbms_output.put_line ('XXX=1') ;
dbms_output.put_line (c1_rec.tname) ;
dbms_output.put_line ('===========================') ;
else
dbms_output.put_line ('===========================') ;
end if ;
end do_it ;
end Y1;
SQL> set serveroutput on
SQL> exec Y1.do_it (0) -- etc -
My procedure takes in a schema and get some data related to that schema. I want ot count the number of rows for each table and table partition stored in the table object_size_history. I was looking into adding a REF CURSOR so I can loop through the tables, table partitions. The Loop is not working for me. I want to get the count using this syntax: select count(*) FROM <schema name>.<table name> INTO <VARIABLE>. How can I achieve this? I attempted this at the end of the procedure
CREATE OR REPLACE PROCEDURE calc_object_size (v_schema_name in varchar2) IS
TYPE type_cur IS REF CURSOR;
cur_rows type_cur;
lv_num_rows number := 0;
lv_obj_name varchar2(30);
BEGIN
INSERT INTO object_size_history (schema_name, object_name, object_type, object_size_mb, count_size_date)
SELECT OWNER, SEGMENT_NAME, SEGMENT_TYPE, BYTES/(1024*1024), current_timestamp from dba_segments
WHERE owner = v_schema_name
AND segment_type IN ('TABLE','INDEX');
COMMIT;
INSERT INTO object_size_history (schema_name, object_name, partition_name, object_type, object_size_mb, count_size_date)
SELECT OWNER, SEGMENT_NAME, partition_name, SEGMENT_TYPE, BYTES/(1024*1024), current_timestamp from dba_segments
WHERE owner=v_schema_name
AND segment_type IN ('TABLE PARTITION','INDEX PARTITION');
COMMIT;
--Now we need a loop that counts the rows for object_type = tables and table partitions, and inserts the count into the object_size_history
OPEN cur_rows FOR
select object_name
from object_size_history
where object_type in ('TABLE', 'TABLE PARTITION');
LOOP
FETCH cur_rows lv_obj_name
SELECT count(*) FROM v_schema_name.lv_obj_name
INTO lv_num_rows;
UPDATE object_size_history
SET object_count = lv_num_rows
WHERE object_name = lv_obj_name
AND object_type IN ('TABLE', 'TABLE PARTITION');
END LOOP;
CLOSE cur_rows;
END;
/Well your specific syntax error is
FETCH cur_rows INTO lv_obj_name;The whole procedure is a bit of a mess though. You don't need to use a ref cursor for what you are attempting. A normal cursor with the FOR UPDATE OF clause is a better bet. You do have to use dynamic SQL for the other selects, because we can't use a variable in a FROM clause.
something like this...
CREATE OR REPLACE PROCEDURE calc_object_size (v_schema_name in varchar2) IS
lv_num_rows number := 0;
lv_obj_name varchar2(30);
CURSOR tabs IS
select object_name
from object_size_history
where object_type in ('TABLE', 'TABLE PARTITION')
FOR UPDATE OF object_count;
BEGIN
INSERT INTO object_size_history (schema_name, object_name, object_type, object_size_mb, count_size_date)
SELECT OWNER, SEGMENT_NAME, SEGMENT_TYPE, sum(BYTES/(1024*1024)), current_timestamp
from dba_segments
WHERE owner = v_schema_name
AND segment_type IN ('TABLE','INDEX')
GROUP BY OWNER, SEGMENT_NAME, SEGMENT_TYPE, current_timestamp;
INSERT INTO object_size_history (schema_name, object_name, partition_name, object_type, object_size_mb, count_size_date)
SELECT OWNER, SEGMENT_NAME, partition_name, SEGMENT_TYPE, sum(BYTES/(1024*1024)), current_timestamp
from dba_segments
WHERE owner=v_schema_name
AND segment_type IN ('TABLE PARTITION','INDEX PARTITION')
GROUP BY OWNER, SEGMENT_NAME, SEGMENT_TYPE, current_timestamp;
OPEN tabs;
FETCH tabs INTO lv_obj_name;
LOOP
EXIT WHEN tabs%NOTFOUND
EXECUTE IMMEDIATE
'SELECT count(*) FROM '||v_schema_name||'.'||lv_obj_name
INTO lv_num_rows;
UPDATE object_size_history
SET object_count = lv_num_rows
WHERE CURRENT OF tabs;
END LOOP;
CLOSE tabs;
COMMIT;
END;
/Cheers, APC -
How to do a 'select count' without a cursor
I am trying to do a select which will return exactly one row, such as:
select count(*) from orders;
Since there is inherent overhead in using a cursor, I am trying to find a way to do this without
generating a result set (both executeQuery and execute use result sets to return the data).
What I am looking for is something equivalent to ESQL:
select count(*) from orders INTO integervariable;
Does anyone know how to do this?The reason for the concern is because I am issuing the
SQL many times (thousands), with a slightly different
'where' clause each time, and the platform I am using
doesn't support 'prepare's.
So, even ignoring the overhead question, is there a
way to do this without using a result set, which
has an inherent cursor.No - there is no way of doing this without using a resultset.
Maybe you are looking for
-
How do i restrict access on ipad for public use?
My employer would like to set up Ipads in each office to display our advertizements and products as well as play informative videos. Eventually we would also like to be able to set up access on the ipads for the clients to type in their personal info
-
Can't see any photos in media browser in other apps
I have my iPhoto library on an external drive due to its size as well as multiple user profiles on this box. No problem when I open iPhoto everything shows up all nice and organized, but if I'm working in another app that uses the media browser to al
-
This past semester I spilled some soda on my laptop and it got inside my keyboard and I had to replace it. My computer just went out of warranty so I made the repairs at home with a friend who knew what he was doing. We took computer apart, cleaned o
-
Change Rows displayed in report
I Have few reports which I wish to show ALL returned rows in a single page (HTML), however I can not find a way to change the number of rows returned other then a Registry edit for all reports. Is htere a way to configure a single report to display
-
Condition Dependent Document Output
Hello, all Please help me, We have extended classic scenario in SRM 5.0. May I to use Condition Dependent Document Output in Spro if I need to print 2 smartforms for one purchase order? Best regards, Guzal