Search code examples
oracleplsqlcursor

getting last record of a cursor


If I have a cursor cur which contain these records

DEPTNO ENAME  ORD   CNT                        
10     KING   1      3                      
10     CLARK  2      3                      
10     MILLER 3      3                      
20     JONES  1      5 

I get my cursor record like this :

FOR i IN cur LOOP
  --Process
END LOOP;

Now I need to enhance my process and do a check, if the value of CNT column of the last record is equal to 5 I don't need to navigate into this cursor.

Is there a way to directly get the last record of the cursor to test CNT column without looping ?


Solution

  • No. A cursor is a pointer to a program that executes the query. You can only fetch from the cursor. Oracle itself has no idea what the last row the cursor will return until you attempt to fetch a row and find there are no more rows to return.

    You could, of course, modify the query so that the CNT of the last row is returned in a separate column (assuming that you have some way to order the rows so that the "last row" is a meaningful concept).