открытый цикл в oracle: выяснить; текущее чтение, следующее чтение, последнее чтение?

#sql #oracle

#sql #Oracle

Вопрос:

    FOR rec IN (SELECT t.*
                 FROM tableCustomers t
                WHERE t.CustomerNo = p_Cust
                  AND t.NameNo = p_EkNo
                  AND t.Type = 1
                  )

   LOOP
-- Here I need to know how to find out,
-- the first read, the next read and the last read ?
   END LOOP;
  

Ответ №1:

 FOR rec IN (SELECT t.*, 
                   rownum as rn, 
                   count(*) over () as cnt
                 FROM tableCustomers t
                WHERE t.CustomerNo = p_Cust
                  AND t.NameNo = p_EkNo
                  AND t.Type = 1
                  )

   LOOP
   if  rec.rn==1 then 
     dbms_output.put_line('first line!');
   end if;

   if  rec.rn > 1 then 
     dbms_output.put_line('after first line!');
   end if;

   if rec.rn = cnt then
     dbms_output.put_line('lastline!');
   end if;

   END LOOP;