Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Get count in for loop cursor

XandotAug 17 2017 — edited Aug 18 2017

Hello,

I have created one for loop cursor and trying to get a count of records in a loop like:

declare

v_count number := 0;

v_char varchar2(2000);

begin

for i in (select count(*) over() as l_count,e.empno,e.ename,e.job,e.sal,e.deptno from emp e) loop

v_count := v_count + 1;

v_char := v_char||','||i.ename;

   if v_count = i.l_count then

     dbms_output.put_line('error msg'||'-->'||ltrim(v_char,',')); -- I want to raise exception here to get error message in orignal requirment

   end if;

end loop;

end;

Above code is working fine but is it ok if we consider the performance of the query or is it a right way to get a count in for loop cursor?

Is there any other way to do the same in for loop cursor?

Thanks for the help.

This post has been answered by BluShadow on Aug 17 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 14 2017
Added on Aug 17 2017
8 comments
10,962 views