循环调用修正sic86 2改后的(除了第一年有点诡异,其他年份可以正常修复)

create or replace procedure rebuild_sic86_wyl(pi_aac001 in number,
                                              po_fhz    out varchar2,
                                              po_msg    out varchar2) is
  --1.根据账户类型来判断是本地的还是转入的,
  --2.如果是本地的,
  --    写一个游标存放sic86.截止上年末缴费月数jzsnm,本年度缴费月数bn,本年累计缴费月数bnnj,
  --    从最小的年份开始,清空最小年费的下一年jzsnm,bnlj
  --    update sic86 set jzsnm = null ,bnlj = null where aae001 >min(aae001)
  --    update 最小年费下一年的 jzsnm = 最小年份bn+bnlj
  v_count      number(4);
  v_zhlx       sic86.zhlx%type;
  v_aae001_min sic86.aae001%type;
  v_aae001_max sic86.aae001%type;
  v_nf         sic86.aae001%type;
  v_bn         sic86.bn%type;
  v_bnlj       sic86.bnlj%type;
  v_jzsnm      sic86.jzsnm%type;
  cursor c_zhlx_list is
    select zhlx from sic86 where aac001 = pi_aac001;
begin
  po_fhz := '1';
  po_msg := '修正成功';
  for rec_zhlx in c_zhlx_list loop
    --选取最小年份和最大年份
    select min(aae001)
      into v_aae001_min
      from sic86
     where aac001 = pi_aac001;
    select max(aae001)
      into v_aae001_max
      from sic86
     where aac001 = pi_aac001;
    if rec_zhlx.zhlx = '0' then
      for nf in v_aae001_min .. v_aae001_max loop
      --去最小年份的本年缴费月数
        select bn
          into v_bn
          from sic86
         where aac001 = pi_aac001
           and aae001 = nf; 
      --取最小年份的本年累计月数
        select bnlj
          into v_bnlj
          from sic86
         where aac001 = pi_aac001
           and aae001 = nf; 
      --最小年份下一年 v_nf
          if (v_nf < v_aae001_max) then
          v_nf := nf + 1; 
          else v_nf := nf;
        end if;
      
        -- 修正1 最小年份下一年的的  jzsnm = 最小年度bn+最小年度bnlj
        update sic86
           set jzsnm =
               (v_bn + v_bnlj)
         where aac001 = pi_aac001
           and aae001 = v_nf;
          
         select 1 into v_count from dual;--测试用,用完后注释掉
      --取最小年份下一年的的截至上年末月数
          select nvl(jzsnm,0)
          into v_jzsnm 
          from sic86
         where aac001 = pi_aac001
           and aae001 = v_nf;
        -- po_msg :=v_nf||'年份的jzsnm='||v_jzsnm;
        --return;         --调试用,正式用的时候注释掉
        --去最小年份下一年的本年缴费月数   
        select bn
          into v_bn
          from sic86
         where aac001 = pi_aac001
           and aae001 = v_nf; 
      
        --清空最小年份下一年的 截至上年末月数 和 本年累计(其实清空不情况无所谓,不影响)
        /*
          update sic86
            set jzsnm = null, bnlj = null
          where aac001 = pi_aac001
            and aae001 = v_nf;
        */
      
        -- 修正2 最小年份下一年的 本年累计月数 bnlj = 本年的jzsnm+本年的bn
        update sic86
           set bnlj =
               (v_jzsnm + v_bn)
         where aac001 = pi_aac001
           and aae001 = v_nf;
      end loop;
      po_fhz := '1';
      po_msg := '循环修正成功';
    elsif rec_zhlx.zhlx = '1' then
      po_fhz := '-1';
      po_msg := '转入的暂不处理';
      return;
    end if;
  end loop;
end;

  

原文地址:https://www.cnblogs.com/Sunnor/p/4653073.html