数组操作

create or replace procedure SFGL_XF_SFBZ(p_xn     in varchar2,
                                         p_czr    in varchar2,
                                         o_errMsg out varchar2) is
  /**
  *  生成收费标准
  *  功能:按学年生成收费标准。1、生成新年级的收费标准;2、已经毕业的年级不生成。

  *  2016年10月21日
  *   p_xn 学年
  *   p_czr 当前操作人
  *   o_errMsg 返回出错信息
  */
  v_xn1 varchar2(10);  --记录学年的第一个年份 eg:2016-2017 中的2016
  before_Xn varchar2(10); --记录前一个学年
  type t_arr is varray(3) of varchar2(20);  --年级数组
  v_nj t_arr;
  v_count number := 0;  --记录查询条数

begin
  v_xn1 := substr(p_xn, 1, 4);  --eg:2017
  before_Xn := (v_xn1-1)||'-'||v_xn1;  --eg:2016-2017
  v_nj := t_arr(null, null,null);
  v_nj(1) := v_xn1-2;  --eg:2015
  v_nj(2) := v_xn1-1;  --eg:2016
  v_nj(3) := v_xn1;  --eg:2017
  
  --判断前两个年级的学年数据是否已经生成
  for x in 1..v_nj.count-1 loop
      v_count :=0;
      select count(1) into v_count from sfgl_xf t where t.xn=p_xn and t.nj = v_nj(x);
      if v_count = 0 then
         insert into sfgl_xf(xfid,yxdm,zydm,fy,modified_time,modified_by,create_time,create_by,nj,zsfy,xn,ybfy)
         select Xl_Sfgl_Xf.NEXTVAL,t.yxdm,t.zydm,t.fy,sysdate,p_czr,sysdate,p_czr,t.nj,t.zsfy,p_xn,t.ybfy from sfgl_xf t where t.xn=before_Xn and t.nj = v_nj(x);
      end if;    
      
  end loop;
  
  --生成最新年级的数据,以上一个学年的最新年级为准 
  --eg:2016-2017的2016年级数据 参考 2015-2016的2015级数据
  v_count :=0;
   select count(1) into v_count from sfgl_xf t where t.xn=p_xn and t.nj = v_nj(3);
    if v_count = 0 then
       insert into sfgl_xf(xfid,yxdm,zydm,fy,modified_time,modified_by,create_time,create_by,nj,zsfy,xn,ybfy)
       select Xl_Sfgl_Xf.NEXTVAL,t.yxdm,t.zydm,t.fy,sysdate,p_czr,sysdate,p_czr,v_nj(3),t.zsfy,p_xn,t.ybfy from sfgl_xf t where t.xn=before_Xn and t.nj = v_nj(2);
    end if;  
    
         
exception
  when others then
    o_errMsg := '程序运行出现内部错误,请联系管理员。';
    raise;
end SFGL_XF_SFBZ;
type t_arr is varray(3) of varchar2(20);  --年级数组

猜你喜欢

转载自563432906.iteye.com/blog/2342039