关键字(5):cursor游标:(循环操作批量数据)

declare 
 cursor stus_cur is select * from students;  --定义游标并且赋值(is 不能和cursor分开使用) 
  cur_stu students%rowtype;                  --定义rowtype                   
 begin   
   open stus_cur;                            --开启游标 
      loop                                   --loop循环 
        exit when stus_cur%notfound;         --循环条件   
        fetch stus_cur into cur_stu;         --游标值赋值到rowtype  
        dbms_output.put_line(cur_stu.name);  --输出     
      end loop ;                              --结束循环   
   close stus_cur;                           --关闭游标
end;   
plsql是面向过程的语言,这类语言还有c,cobol等,这类语言的共同点是一次只能处理一条数据,而数据库sql返回的对象是一个集合,这样直接用plsql程序操作就会出现问题。在这种环境下就出现了游标,游标实际是一个内存地址,指向的是sql查询出的结果集,当需要的时候再根据游标一条一条取数据【fetch】,直到全部数据取完。
CURSOR minInter1 IS
   SELECT t1.rm_interface,
          t1.rm_merchno,
          min(GetExp(t1.rr_id, TradeAmount, '' , '' , '0' )) minExp
     FROM t_r_merchant t1
    WHERE t1.rm_interface = InterId1
       OR t1.rm_interface = InterId2
    GROUP BY t1.rm_interface, t1.rm_merchno;

OPEN minInter1;
  LOOP
    EXIT WHEN minInter1%NOTFOUND ;
    FETCH minInter1 INTO nResult, tmpMchNo, minexp;
    iCount := minInter1% rowcount;
  END LOOP;
CLOSE minInter1;
 
其实可以用数据集合代替游标——wyp20141010
原文地址:https://www.cnblogs.com/bitter-first-sweet-last/p/3964119.html