Hooray for me.
Since the "count(*) over ()" tells you the number of rows in the result set, you can use it within the loop to find out if you have reached the last record or not.Code:SQL> create table my_table (col1 number, col2 number,col3 number); Table created. SQL> DECLARE 2 CURSOR my_cur IS 3 SELECT column_name ,count(*) over () tot_rows FROM user_tab_columns 4 WHERE table_name=UPPER('&1'); 5 BEGIN 6 FOR rec IN my_cur LOOP 7 IF my_cur%ROWCOUNT = 1 THEN 8 dbms_output.put_line('FIRST_ROW '||rec.column_name); 9 ELSIF my_cur%rowcount = rec.tot_rows then 10 dbms_output.put_line('LAST_ROW '||rec.column_name); 11 END IF; 12 END LOOP; 13* END; SQL> / Enter value for 1: MY_TABLE old 4: WHERE table_name=UPPER('&1'); new 4: WHERE table_name=UPPER('MY_TABLE'); FIRST_ROW COL1 LAST_ROW COL3 PL/SQL procedure successfully completed.




Reply With Quote