oracle 儲存過程 游標巢狀

2021-07-04 20:34:37 字數 2245 閱讀 9069

這是一段將裝置的gps資料的工作小時數進行統計的儲存過程,首先通過游標取出所有裝置資訊,然後對每一台裝置進行工作小時數統計,其中用到了游標巢狀

create or replace procedure cmtools.proc_statistics_workhour is

v_begindate date;

v_enddate date;

v_recordcount number;

v_recordindex number;

v_statistical_uuid char(36);

v_workhour number;

v_gpsdate date;

v_equipid char(36);

/*定義游標 取出所以安裝gps的裝置*/

cursor c_equiprows is select equip_id from equip_equipment where gps_type>0;

begin

select count(*) into v_recordcount from gps_statistical_date;

if v_recordcount>0 then

begin

select max(statistical_date)+1 into v_begindate from gps_statistical_date;

end;

else

begin

select min(gps_date) into v_begindate from gps_monitor_data;

end;

end if;

v_begindate := to_date(to_char(v_begindate,'yyyy-mm-dd') || ' 00:00:00','yyyy-mm-dd hh24:mi:ss');

while v_begindate < sysdate-1 loop

begin

v_enddate := to_date(to_char(v_begindate,'yyyy-mm-dd') || ' 23:59:59','yyyy-mm-dd hh24:mi:ss');

begin

select get_uuid() into v_statistical_uuid from dual;

insert into gps_statistical_date values(v_statistical_uuid,to_date(to_char(v_begindate,'yyyy-mm-dd'),'yyyy-mm-dd'));

for equiprow in c_equiprows loop

begin

v_equipid := equiprow.equip_id;

v_recordindex:=1;

v_workhour:=0;

for gpsrow in (select * from table(fun_workhour_dt(v_equipid,v_begindate,v_enddate))) loop

begin

if v_recordindex mod 2 = 0 then

begin

v_workhour := v_workhour + round((gpsrow.gps_date-v_gpsdate)*24,2);

end;

end if;

v_recordindex := v_recordindex+1;

v_gpsdate := gpsrow.gps_date;

end;

end loop;

insert into gps_statistical_workhour values(get_uuid(),v_statistical_uuid,equiprow.equip_id,v_workhour);

end;

end loop;

end;

v_begindate := v_begindate+1;

end;

end loop;

exception

when others then

rollback;

end proc_statistics_workhour;

儲存過程 游標巢狀迴圈

alterprocedure dbo asdeclare startnum nvarchar 255 declare endnum nvarchar 255 declare insurancetype nvarchar 255 declare company id nvarchar 255 decl...

oracle儲存過程,游標

oracle儲存過程,游標 2010 07 07 13 01 create or replace procedure p tb task log is 功能 插入任務到任務日誌表 v task start date date v task end date date v sql code numbe...

oracle 儲存過程 游標

create or replace procedure exception3 as 使用者自定義異常 e too high sal exception 宣告自定義異常 v sal employees.salary type begin select salary into v sal from em...