Oracle 游標迴圈插入資料

2021-08-28 10:15:09 字數 4714 閱讀 7601

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

先查出每個月月底的日期作為條件

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;

迴圈上面的結果依次作為查詢條件查出資料插入表

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; --結束

/ --這個斜槓用處很大,比如好多條儲存過程的話,可以寫在後面一起執行。

mysql游標遍歷迴圈 插入資料

begin declare no more record int default 0 declare insertcolumn varchar 18 declare cur record cursor for select insertparam from testtable declare con...

oracle 採用游標迴圈插入資料的儲存過程

用case when else判斷是否插入隨機生成的考勤記錄 create or replace procedure mytest restorekq as kssj date jssj date tmpsj date amnum int pmnum int cursor cs is select ...

Oracle 迴圈插入資料 Demo

oracle 迴圈插入資料 demo declare maxnumber constant int 1000 i int 1 begin for i in 1.maxnumber loop insert into studentinformation id name address,hobby va...