游标使用

简单游标

declare
  cursor cur_pol is
    select m.policyno, m.companycode, m.productcode
      from nbz_policy_main m
     where m.inputdate < sysdate
       and m.inputdate > date '2015-3-15';
      v_p cur_pol%rowtype;
begin
  open cur_pol;
  loop
    fetch cur_pol into v_p;
    exit when cur_pol%notfound;
      dbms_output.put_line(v_p.policyno);
  end loop;
  close cur_pol;
end;


使用bulk collect

declare
  cursor cur_pol is
    select m.policyno, m.companycode, m.productcode
      from nbz_policy_main m
     where m.inputdate < sysdate
       and m.inputdate > date '2015-3-15';
  type t_p is table of cur_pol%rowtype;
  v_p t_p;
begin
  open cur_pol;
  loop
    fetch cur_pol bulk collect
      into v_p limit 10;
    exit when v_p.count = 0;
    forall i in 1 .. v_p.count 
      update nbz_policy_main t set t.lastmodifydate = sysdate where t.policyno = v_p(i).policyno;
      dbms_output.put_line(sysdate);
    commit;
  end loop;
  close cur_pol;
end;



隐性游标
declare
begin
  for pi in (select m.policyno, m.companycode, m.productcode
               from nbz_policy_main m
              where m.inputdate < sysdate
                and m.inputdate > date '2015-3-15') loop  
    dbms_output.put_line(pi.policyno);
  end loop;
end;

猜你喜欢

转载自lysunki.iteye.com/blog/2202930