在oracle上打开循环:找出;当前读取,下次读取,最后读取?

时间:2011-10-24 06:54:07

标签: 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 个答案:

答案 0 :(得分:3)

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;