create or replace procedure test(pi_yyyy_mm in varchar2) is
  l_ct number;
  function get_continuouscreditcardnumber(pi_cardno varchar2,
                                          pi_date   varchar2) return number is
    l_cont_ct number;
  begin
    select nvl(max(continuouscreditcardnumber), 0) + 1
      into l_cont_ct
      from t
     where t.cardno = pi_cardno
       and to_char(t.slotcarddate + 1, 'yyyymmdd') = pi_date;
    return l_cont_ct;
  exception
    when others then
      l_cont_ct := 1;
      return l_cont_ct;
  end;
begin
  for c1 in (select s.cardno,
                    to_char(s.slotcarddate, 'yyyymmdd') dt,
                    sum(s.slotcardmoney) tot_money,
                    count(s.cardno) cnt
               from s
              where to_char(s.slotcarddate, 'yyyymm') = pi_yyyy_mm
              group by s.cardno, to_char(s.slotcarddate, 'yyyymmdd')
              order by s.cardno, to_char(s.slotcarddate, 'yyyymmdd')) loop
    l_ct := get_continuouscreditcardnumber(c1.cardno, c1.dt);
    insert into t
    values
      (to_date(c1.dt, 'yyyymmdd'), c1.cardno, c1.cnt, c1.tot_money, l_ct);
  end loop;
  commit;
end test;

解决方案 »

  1.   

    这里的C1不用定义,相当于取游标的里的一条记录。
      

  2.   

    请问是指输入一个月份,就只查当月的信息吗?还是如果连续的话,非当月的也查出来、
    表可是有1-3月的信息的
      

  3.   

    嗯,是的    输入一个月份  只查询当月的信息。
      

  4.   

    可以写一下注释吗?   好让我理解每一句的意思!
      

  5.   

    刚接处存储过程,有点看不太懂!!!!!!!!
      

  6.   

    看不懂,慢慢看呗。不是很难的,这个过程用到函数和游标,看看这方面知识