Home » SQL & PL/SQL » SQL & PL/SQL » cursor length
cursor length [message #121678] Tue, 31 May 2005 07:54 Go to next message
wojtyla
Messages: 17
Registered: March 2005
Location: USA
Junior Member

How do i find the length of a cursor in PLSQL
pretty urgent

[Updated on: Tue, 31 May 2005 07:59]

Report message to a moderator

Re: cursor length [message #121685 is a reply to message #121678] Tue, 31 May 2005 09:00 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
What do you mean under "length of cursor" ? If you mean
nuber of fetched/processed rows there is %ROWCOUNT cursor attribute:

SQL> declare
  2   cursor t1 is select empno from emp;
  3   empno emp.empno%type;
  4  begin
  5   open t1;
  6   loop
  7    fetch t1 into empno;
  8    exit when t1%notfound;
  9    dbms_output.put_line(t1%rowcount);
 10   end loop;
 11   close t1;
 12  end;
 13  /
1
2
3
4
5
6
7
8
9
10
11
12
13
14

PL/SQL procedure successfully completed.

SQL> begin
  2   update emp set sal=sal where sal < 3000;
  3   dbms_output.put_line(sql%rowcount || ' rows updated');
  4  end;
  5  /
11 rows updated

PL/SQL procedure successfully completed.


Rgds.
Re: cursor length [message #121687 is a reply to message #121678] Tue, 31 May 2005 09:05 Go to previous messageGo to next message
saadatahmad
Messages: 452
Registered: March 2005
Location: Germany/Paderborn
Senior Member

By length, if you mean how many rows a cursor will return then we use %ROWCOUNT attribute for this purpose.

SQL> DECLARE
  2   CURSOR c1 IS
  3   SELECT empno, ename
  4   FROM emp
  5   WHERE deptno = 10;
  6   c2 c1%ROWTYPE;
  7  BEGIN
  8   OPEN c1;
  9    LOOP
 10     FETCH c1 INTO c2;
 11     EXIT WHEN c1%NOTFOUND;
 12    END LOOP;
 13   DBMS_OUTPUT.PUT_LINE('Cursor will return '||c1%ROWCOUNT||' rows.');
 14   CLOSE c1;
 15  END;
 16  /
Cursor will return 3 rows.

PL/SQL procedure successfully completed.

[Updated on: Tue, 31 May 2005 09:10]

Report message to a moderator

Re: cursor length [message #121862 is a reply to message #121687] Wed, 01 June 2005 08:59 Go to previous messageGo to next message
wojtyla
Messages: 17
Registered: March 2005
Location: USA
Junior Member

i want to know the count before going into the loop.
can you ppl help me with that.
Re: cursor length [message #121871 is a reply to message #121862] Wed, 01 June 2005 09:49 Go to previous messageGo to next message
kamaltherocky
Messages: 3
Registered: June 2005
Location: Chennai India
Junior Member
Hi

There is no way to Fetch the Count of a Cursor before you fetch.
But there is a work around by using a standalone SQL which gives the count. For Example,

DECLARE
CURSOR c1 IS
SELECT empno, ename
FROM emp
WHERE deptno = 10;
c2 c1%ROWTYPE;
v_count Number;
BEGIN

Select Count(*) into v_count
From Emp
Where deptno = 10;

DBMS_OUTPUT.PUT_LINE('Cursor will return '||v_count||' rows.');
OPEN c1;
LOOP
FETCH c1 INTO c2;
EXIT WHEN c1%NOTFOUND;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Cursor will return '||c1%ROWCOUNT||' rows.');
CLOSE c1;
END;

Regards
kamal
Re: cursor length [message #121884 is a reply to message #121678] Wed, 01 June 2005 10:58 Go to previous message
Frank
Messages: 7901
Registered: March 2000
Senior Member
select an extra column "count(*) over ()" in your query. This way you don't have to parse two different queries, but you'll get the total number of records with your first (actually with every) record.
SQL> create table t as select rownum id from user_objects where rownum < 10;

Table created.

SQL> select id
  2  ,      count(*) over () as number_of_rows
  3  from   t
  4  /

        ID NUMBER_OF_ROWS
---------- --------------
         1              9
         2              9
         3              9
         4              9
         5              9
         6              9
         7              9
         8              9
         9              9

9 rows selected.


hth

[edit: added example]

[Updated on: Wed, 01 June 2005 11:00]

Report message to a moderator

Previous Topic: Converting into Date
Next Topic: review questions
Goto Forum:
  


Current Time: Fri Apr 25 23:40:24 CDT 2025