解决方案 »

  1.   

    这个是我调用 存储过程
    create or replace procedure vip_recharge(in_uid  in integer) 
    is
      c_rd_id  recharge_detail.rd_id%type;
      c_u_id   recharge_detail.u_id%type;
      c_pay_time recharge_detail.pay_time%type;
      c_year_num recharge_detail.year_num%type;
      
      cursor vip_cursor is
      select rd_id,u_id,pay_time,year_num
      from recharge_detail 
      where u_id=in_uid
           and pay_off='1'  --是否付清
      order by rd_id;
           
    begin
      open vip_cursor;
          loop
             fetch vip_cursor into  c_rd_id,c_u_id,c_pay_time,c_year_num;         --loop and fench
             exit when vip_cursor%notfound;
                    
             merge into user_vip ma      
                  using (select b.u_id,
                                nvl(a.u_id,0) aid,      --用户名编号不存在设为0                      
                                case when c_pay_time>nvl(a.end_time,to_date('1970-01-01','yyyy-mm-dd')) then c_pay_time
                                     else a.begin_time  --支付时间大于结束时间(结束时间为null时即为1970-01-01)则为支付时间否则为结束时间作为vip的开始时间
                                end  vip_begin_time,
                                case when sysdate>nvl(a.end_time,to_date('1970-01-01','yyyy-mm-dd')) then add_months(sysdate, 12*c_year_num)
                                     else add_months(a.end_time, 12*c_year_num)  --系统当前时间大于结束时间(结束时间为null时即为1970-01-01)则系统当前时间添加年限(12*c_year_num),否则结束时间添加年限作为vip结束时间
                                end  vip_end_time,
                                b.year_statis
                         from user_vip a right join 
                             (select u_id,sum(year_num) year_statis
                             from recharge_detail
                             where u_id=c_u_id
                             and pay_off>0     --pay_off 0 1 2 
                             group by u_id)b
                             on  a.u_id=b.u_id) mb
                    on (ma.u_id=mb.u_id and ma.u_id=mb.aid)   --用户会员表中的用户编号和recharge_detail信息变更表中的信息相同时匹配
               when matched then      
                    update set ma.begin_time=mb.vip_begin_time,
                               ma.end_time=mb.vip_end_time,
                               ma.year_statis=mb.year_statis   --设置
               when not matched then 
                    insert (u_id,begin_time,end_time,year_statis) 
                    values(c_u_id,sysdate,add_months(sysdate,12*c_year_num),c_year_num);  --添加
                    
           update recharge_detail set pay_off=2 where rd_id=c_rd_id;    --修改     
           commit;  
          end loop;
       close vip_cursor;end vip_recharge;
    --通过用户id