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!

FOR LOOP - PL/SQL

oracleSep 5 2006 — edited Sep 5 2006

Hi,

I have joined a new place and I am a bit surprised to see almost every SQL statement is inside a FOR LOOP cursor. The best example would be a sequence fetch. This too is inside a FOR LOOP. I have provided the code below.

Can someone pls explain whether there is any reason to do so ? Can't we fetch the same using a simple select statement "SELECT DAILY_SUMMARY.nextval INTO nNextSummaryId FROM DUAL" ? What will be the performance impact ?

FUNCTION   GetNextSummaryId   RETURN NUMBER  IS
 nNextSummaryId Number(15);
CURSOR Cur_Next_Sum_Id IS
   SELECT DAILY_SUMMARY.nextval next_val
   FROM   DUAL;
BEGIN
 FOR tmp_cur In Cur_Next_Sum_Id LOOP
    nNextSummaryId :=  tmp_cur.next_val;
    EXIT ;
 END LOOP ;
  RETURN(nNextSummaryId) ;
EXCEPTION
  WHEN OTHERS THEN
      RETURN(NULL) ;
END;
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 3 2006
Added on Sep 5 2006
3 comments
749 views