1. 程式人生 > >Oracle-遊標迴圈插入資料

Oracle-遊標迴圈插入資料

遇到一個需求統計歷史每個月底的資料插入到表中,查詢了資料發現使用遊標會很方便,記錄一下解決思路

  1. 先查出每個月月底的日期作為條件
select to_char(lastday, 'yyyy-mm-dd') lastday
  from (select last_day(add_months(to_date('201408', 'yyyymm'), level)) lastday
          from dual
        connect by level <= 50)
 order by lastday desc;
  1. 迴圈上面的結果依次作為查詢條件查出資料插入表
declare  --宣告兩個變數
  v_date VARCHAR2(50);
  cursor yb is
    select to_char(lastday,'yyyy-mm-dd') lastday from (
    select last_day(add_months(to_date('201408','yyyymm'),level)) lastday from dual connect by level<=50
    ) order by lastday desc;
begin
  open yb;   --開啟遊標
  loop       --開始標記
    fetch yb into v_date;--遊標賦值  當然這邊可以賦值多個值
    exit when yb%notfound;--遊標一條一條地遍歷記錄,當找不到記錄時退出
    begin
      insert into business_info
  (id,
   LOANAMOUNT,
   LOANCOUNT,
   LOANBALANCE,
   LOANBALANCENUM,
   INTEREST,
   BIDMEMCOUNT,
   LOANMEMCOUNT,
   BIDMEMNUM,
   LOANMEMNUM,
   TENPER,
   MAXPER,
   counttime)
select business_info_id.nextval id, a.*,b.*,c.*,d.*,e.*,f.*,g.*,h.*,i.*,to_timestamp(v_date,'yyyy-mm-dd hh24:mi:ss') as counttime from 
(
SELECT nvl(sum(loanamount), 0) as loanamount, count(1) loancount
  FROM p_loan
 WHERE status IN (06, 07)
   AND loantype = 0  
   and to_char(createtime,'yyyy-mm-dd')<=v_date
)a,
(
select nvl(sum(rprincipal), 0) as yumoney
  from repayment
 where status in ('0', '1')
   and isflag = '1'
   and to_char(createtime,'yyyy-mm-dd')<=v_date
)b,
(
select count(1) as zdbs
  from p_loan
 where loantype = '0'
   and status = '06'
   and to_char(createtime, 'yyyy-mm-dd') <= v_date
)c,
(
Select Nvl(Sum(Money),'0') as yulixi--利息餘額
  From (Select Rp.Bidmemberid,
               Rp.Cloanid,
               Sum(Nvl(Money, 0) + Nvl(Money1, '0')) As Money
          From (Select Sum(a.Srinterest) As Money, a.Bidmemberid, a.Cloanid
                  From Repayment a
                 Where a.Status In ('0', '1')
                   And a.Isflag = '1'
                   and to_char(createtime,'yyyy-mm-dd')<=v_date
                 Group By a.Bidmemberid, a.Cloanid) Rp
          Left Join (Select Nvl(Sum(b.Sramount), 0) As Money1,
                           b.Cloanid,
                           b.Memberid
                      From Repaymentcost b
                     Where b.Status In ('0', '1')
                       And b.Isflag = '1'
                       and to_char(createtime,'yyyy-mm-dd')<=v_date
                     Group By b.Cloanid, b.Memberid) Rt
            On Rp.Cloanid = Rt.Cloanid
           And Rp.Bidmemberid = Rt.Memberid
         Group By Rp.Bidmemberid, Rp.Cloanid)
)d,
(
Select Count(Distinct memberid) As bidmemcount
  from p_bid
 Where status in ('2', '3')
   and to_char(createtime, 'yyyy-mm-dd') <=v_date
)e,
(
select count(distinct(memberid)) as loanmemcount
  from p_loan
 where status in ('06', '07')
   and loantype = '0'
   and to_char(createtime,'yyyy-mm-dd')<=v_date
)f,
(
Select Count(Distinct Bidmemberid) As Tzcount,
       Count(Distinct Memberid) As Jkcount
  From Repayment
 Where Isflag = '1'
   And Status = '0'
    and to_char(createtime,'yyyy-mm-dd')<=v_date
)g,
(
select sum(rate) as tenrate
  from (select decode(b.ramount,0,0,trunc((a.loanamount / b.ramount), 4) * 100) as rate --佔比
          from (select m.enrolname, sum(r.rprincipal) as loanamount
                  from repayment r
                  left join member m
                    on r.memberid = m.memid
                 where r.isflag = '1'
                 and to_char(r.createtime,'yyyy-mm-dd')<=v_date
                 group by m.enrolname
                 order by loanamount desc) a,
               (select nvl(sum(t.rprincipal),'0') as ramount
                  from repayment t
                 where isflag = '1'
                 and to_char(createtime,'yyyy-mm-dd')<=v_date) b)
 where rownum <= 10
)h,
(
select sum(rate) as tenrate
  from (select decode(b.ramount,0,0,trunc((a.loanamount / b.ramount), 4) * 100) as rate --佔比
          from (select m.enrolname, sum(r.rprincipal) as loanamount
                  from repayment r
                  left join member m
                    on r.memberid = m.memid
                 where r.isflag = '1'
                 and to_char(r.createtime,'yyyy-mm-dd')<=v_date
                 group by m.enrolname
                 order by loanamount desc) a,
               (select nvl(sum(t.rprincipal),'0') as ramount
                  from repayment t
                 where isflag = '1'
                 and to_char(createtime,'yyyy-mm-dd')<=v_date) b)
 where rownum <= 1
)i;
    exception    --異常丟擲
      when others then
        dbms_output.put_line(v_date);
    end;
  end loop;  --結束標記
  commit;
  close yb;  --關閉遊標
end;         --結束
/            --這個斜槓用處很大,比如好多條儲存過程的話,可以寫在後面一起執行。