簡單oracle存儲過程,使用遊標

create or replace procedure P_COUNT_MOBILELIST_RLOG 
AS
  tc_success_count number; -- 電信成功總數 
  tc_fail_count number; -- 電信失敗總數 
  tc_un_count number; -- 電信未知總數
  tc_mobilelist_rlog mobilelist_rlog%rowtype; -- 電信表對象
  tc_tmp number;
  -- 定義電信遊標
  cursor tc_cursor is select * from mobilelist_rlog 
  where ch_id in (select id from channel where ch_id= 0) and seq_mobilelist in (select seq from mobilelist) and COUNT_FLAG=0 and rownum < 20000;
  
  cm_success_count number; -- 移動成功總數 
  cm_fail_count number; -- 移動失敗總數 
  cm_un_count number; -- 移動未知總數
  cm_mobilelist_rlog mobilelist_rlog%rowtype; -- 移動表對象
  cm_tmp number;
  -- 定義移動遊標
  cursor cm_cursor is select * from mobilelist_rlog 
  where ch_id in (select id from channel where ch_id= 1) and seq_mobilelist in (select seq from mobilelist) and COUNT_FLAG=0 and rownum < 20000;
  
  un_success_count number; -- 聯通成功總數 
  un_fail_count number; -- 聯通失敗總數 
  un_un_count number; -- 聯通未知總數
  un_mobilelist_rlog mobilelist_rlog%rowtype; -- 聯通表對象
  un_tmp number;
  -- 定義聯通遊標
  cursor un_cursor is select * from mobilelist_rlog 
  where ch_id in (select id from channel where ch_id= 2) and seq_mobilelist in (select seq from mobilelist) and COUNT_FLAG=0 and rownum < 20000;
BEGIN
  -- 開始遍歷電信遊標
  dbms_output.put_line('電信開始'); -- 打印
  tc_success_count:=0;
  tc_fail_count:=0;
  tc_un_count:=0;
  tc_tmp:=0;
  open tc_cursor; -- 打開遊標
  loop
    fetch tc_cursor into tc_mobilelist_rlog; -- 將遊標寫入電信表對象
    EXIT WHEN tc_cursor%NOTFOUND; --遊標取不到數據則退出
    -- 查詢此日期數據是否存在,日期不存在,則做插入操作
    select count(*) into tc_tmp from mobilelist_rlog_tmp where count_date = to_date(to_char(tc_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd'); 
    if (tc_tmp = 0) then 
      insert into MOBILELIST_RLOG_TMP (seq,COUNT_DATE) VALUES ("seq_mobilelist_rlog_tmp".nextVal,to_date(to_char(tc_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd'));
      commit;
      --dbms_output.put_line('插入日期成功');
    end if;
    --exception when others then
      --dbms_output.put_line('some errors');
    --rollback; 
    if (tc_mobilelist_rlog.rpt_str = 'DELIVRD' or tc_mobilelist_rlog.rpt_str = '發送成功。') then -- 發送成功
      tc_success_count:=tc_success_count+1;
      -- 日期存在,則做更新操作
      update MOBILELIST_RLOG_TMP set TC_SUCCSEE = TC_SUCCSEE + 1 where COUNT_DATE = to_date(to_char(tc_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd'); 
      -- 將原表中的這條記錄改爲已統計
      update mobilelist_rlog set COUNT_FLAG = 1 where seq = tc_mobilelist_rlog.seq;
    elsif (tc_mobilelist_rlog.rpt_str != 'DELIVRD' or tc_mobilelist_rlog.rpt_str != '發送成功。' and tc_mobilelist_rlog.rpt_str is not null) then -- 發送失敗
      tc_fail_count:=tc_fail_count+1;
      update MOBILELIST_RLOG_TMP set TC_FAIL = TC_FAIL + 1 where COUNT_DATE = to_date(to_char(tc_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd');
      -- 將原表中的這條記錄改爲已統計
      update mobilelist_rlog set COUNT_FLAG = 1 where seq = tc_mobilelist_rlog.seq;
    elsif (tc_mobilelist_rlog.rpt_str is null) then -- 未知狀態
      tc_un_count:=tc_un_count+1;
      update MOBILELIST_RLOG_TMP set TC_UN = TC_UN + 1 where COUNT_DATE = to_date(to_char(tc_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd');
      -- 將原表中的這條記錄改爲已統計
      update mobilelist_rlog set COUNT_FLAG = 1 where seq = tc_mobilelist_rlog.seq;
    end if;
  commit; 
  end loop;
  close tc_cursor;
  dbms_output.put_line('成功'||tc_success_count); -- 打印
  dbms_output.put_line('失敗'||tc_fail_count); -- 打印
  dbms_output.put_line('未知'||tc_un_count); -- 打印
  dbms_output.put_line('電信結束'); -- 打印
  -- 結束電信遊標
  
  -- 開始移動遊標
  dbms_output.put_line('移動開始'); -- 打印
  cm_success_count:=0;
  cm_fail_count:=0;
  cm_un_count:=0;
  cm_tmp:=0;
  open cm_cursor; -- 打開遊標
  loop
    fetch cm_cursor into cm_mobilelist_rlog; -- 將遊標寫入移動表對象
    EXIT WHEN cm_cursor%NOTFOUND; --遊標取不到數據則退出
    select count(*) into cm_tmp from mobilelist_rlog_tmp where count_date = to_date(to_char(cm_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd'); -- 查詢此日期數據是否存在
    if (cm_tmp = 0) then 
      -- 日期不存在,做新增操作
      insert into MOBILELIST_RLOG_TMP (seq,COUNT_DATE) VALUES ("seq_mobilelist_rlog_tmp".nextVal,to_date(to_char(cm_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd'));
      commit; 
    end if;  
    if (cm_mobilelist_rlog.rpt_str = 'DELIVRD' or cm_mobilelist_rlog.rpt_str = '發送成功。') then -- 發送成功
      cm_success_count:=cm_success_count+1;
      update MOBILELIST_RLOG_TMP set cm_succsee = cm_succsee + 1 where COUNT_DATE = to_date(to_char(cm_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd');
      -- 將原表中的這條記錄改爲已統計
      update mobilelist_rlog set COUNT_FLAG = 1 where seq = cm_mobilelist_rlog.seq;
    elsif (cm_mobilelist_rlog.rpt_str != 'DELIVRD' or cm_mobilelist_rlog.rpt_str != '發送成功。' and cm_mobilelist_rlog.rpt_str is not null) then -- 發送失敗
      cm_fail_count:=cm_fail_count+1;
      update MOBILELIST_RLOG_TMP set cm_fail = cm_fail + 1 where COUNT_DATE = to_date(to_char(cm_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd');
      -- 將原表中的這條記錄改爲已統計
      update mobilelist_rlog set COUNT_FLAG = 1 where seq = cm_mobilelist_rlog.seq;
    elsif (cm_mobilelist_rlog.rpt_str is null) then -- 未知狀態
      cm_un_count:=cm_un_count+1;
      update MOBILELIST_RLOG_TMP set cm_un = cm_un + 1 where COUNT_DATE = to_date(to_char(cm_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd');
      -- 將原表中的這條記錄改爲已統計
      update mobilelist_rlog set COUNT_FLAG = 1 where seq = cm_mobilelist_rlog.seq;
    end if;
  commit; 
  end loop;
  close cm_cursor;
  dbms_output.put_line('成功'||cm_success_count); -- 打印
  dbms_output.put_line('失敗'||cm_fail_count); -- 打印
  dbms_output.put_line('未知'||cm_un_count); -- 打印
  dbms_output.put_line('移送結束'); -- 打印
  -- 結束移動遊標
  
  -- 開始聯通遊標
  dbms_output.put_line('聯通開始'); -- 打印
  un_success_count:=0;
  un_fail_count:=0;
  un_un_count:=0;
  un_tmp:=0;
  open un_cursor; -- 打開遊標
  loop
    fetch un_cursor into un_mobilelist_rlog; -- 將遊標寫入聯通表對象
    EXIT WHEN un_cursor%NOTFOUND; --遊標取不到數據則退出
    select count(*) into un_tmp from mobilelist_rlog_tmp where count_date = to_date(to_char(un_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd'); -- 查詢此日期數據是否存在
    if (un_tmp = 0) then 
      -- 日期不存在,做新增操作
      insert into MOBILELIST_RLOG_TMP (seq,COUNT_DATE) VALUES ("seq_mobilelist_rlog_tmp".nextVal,to_date(to_char(un_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd'));
      commit; 
    end if;  
    if (un_mobilelist_rlog.rpt_str = 'DELIVRD' or un_mobilelist_rlog.rpt_str = '發送成功。') then -- 發送成功
      un_success_count:=un_success_count+1;
      update MOBILELIST_RLOG_TMP set un_succsee = un_succsee + 1 where COUNT_DATE = to_date(to_char(un_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd');
      -- 將原表中的這條記錄改爲已統計
      update mobilelist_rlog set COUNT_FLAG = 1 where seq = un_mobilelist_rlog.seq;
    elsif (un_mobilelist_rlog.rpt_str != 'DELIVRD' or un_mobilelist_rlog.rpt_str != '發送成功。' and un_mobilelist_rlog.rpt_str is not null) then -- 發送失敗
      un_fail_count:=un_fail_count+1;
      update MOBILELIST_RLOG_TMP set un_fail = un_fail + 1 where COUNT_DATE = to_date(to_char(un_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd');
      -- 將原表中的這條記錄改爲已統計
      update mobilelist_rlog set COUNT_FLAG = 1 where seq = un_mobilelist_rlog.seq;
    elsif (un_mobilelist_rlog.rpt_str is null) then -- 未知狀態
      un_un_count:=un_un_count+1;
      update MOBILELIST_RLOG_TMP set un_un = un_un + 1 where COUNT_DATE = to_date(to_char(un_mobilelist_rlog.sendtime,'yyyy-MM-dd'),'yyyy-MM-dd');
      -- 將原表中的這條記錄改爲已統計
      update mobilelist_rlog set COUNT_FLAG = 1 where seq = un_mobilelist_rlog.seq;
    end if;
  commit;
  end loop;
  close un_cursor;
  dbms_output.put_line('成功'||un_success_count); -- 打印
  dbms_output.put_line('失敗'||un_fail_count); -- 打印
  dbms_output.put_line('未知'||un_un_count); -- 打印
  dbms_output.put_line('聯通結束'); -- 打印
  -- 結束聯通遊標
  
  -- 異常處理
  exception when others then
  rollback;
  commit;
  pdmt_errlog(sqlcode,'P_COUNT_MOBILELIST_RLOG',sqlerrm);
  commit;
END P_COUNT_MOBILELIST_RLOG;



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章