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 sfz,zwbh FROM ncldlpx where xxid='0001' and pxqb='2013001';
begin
select pxkssj into kssj from kbqb where xxid='0001' and qb='2013001';
select pxjssj into jssj from kbqb where xxid='0001' and qb='2013001';
for c in cs loop--for循环嵌套while loop循环需插入临时变量。
tmpsj:=kssj;
while tmpsj<=jssj loop
select count(*) into amnum from zw_kq where ksj between to_date(to_char(tmpsj,'yyyy-MM-dd')||' '||'07:00:00','yyyy-MM-dd hh24:mi:ss') and
to_date(to_char(tmpsj,'yyyy-MM-dd')||' '||'09:00:00','yyyy-MM-dd hh24:mi:ss')  and zwbh=c.zwbh and sfz=c.sfz;
select count(*) into pmnum from zw_kq where ksj between to_date(to_char(tmpsj,'yyyy-MM-dd')||' '||'17:00:00','yyyy-MM-dd hh24:mi:ss') and
to_date(to_char(tmpsj,'yyyy-MM-dd')||' '||'19:00:00','yyyy-MM-dd hh24:mi:ss')  and zwbh=c.zwbh and sfz=c.sfz;
case when amnum=0 then insert into zw_kq(ksj,type,zwbh,sfz,qdtype) values(to_date(to_char(tmpsj,'yyyy-MM-dd')||' '||'07:'||round(dbms_random.value(50,59),0)||':'||round(dbms_random.value(0,59),0),'yyyy-MM-dd hh24:mi:ss'),1,c.zwbh,c.sfz,'0');
when pmnum=0 then insert into zw_kq(ksj,type,zwbh,sfz,qdtype) values(to_date(to_char(tmpsj,'yyyy-MM-dd')||' '||'17:'||round(dbms_random.value(00,10),0)||':'||round(dbms_random.value(0,59),0),'yyyy-MM-dd hh24:mi:ss'),1,c.zwbh,c.sfz,'0');
else dbms_output.put_line('正常考勤');

end case;

--while loop 循环中插入case when then 语句块,以end case 为结束标识。

tmpsj:=tmpsj+1;
end loop;
end loop;

end mytest_RestoreKq;


--select dbms_random.value from dual; 

--oracle dbms_random函数自动产生随机数


--oracle dbms_random.value(0,100) 函数自动产生0——100之间随机数

你可能感兴趣的:(数据库)