oracle 存儲過程--遊標

靜態遊標

CREATE OR REPLACE PROCEDURE ORDER_TO_HIS(i_beginTime       in varchar2, --開始時間
                                         i_endTime         in varchar2, -- 結束時間
                                         i_mkt_campaign_id in varchar2 --活動id
                                         ) as
  --年月表後綴

  cursor c_emp is
    select b.contact_order_id
      from contact_order b
     where b.mkt_campaign_id = i_mkt_campaign_id
       and b.create_date >= to_date(i_beginTime, 'yyyy/MM/dd HH24:mi:ss')
       and b.create_date <= to_date(i_endTime, 'yyyy/MM/dd hh24:mi:ss')
       and rownum < 40000;

  order_id contact_order.contact_order_id%type;
  --  根據活動 id 開始時間和結束時間取 contact_order表數據 從而找到 contact_result表和contact_push_msg表數據
  --  先插入年月表,然後刪除contact_result表和contact_push_msg表數據 最後刪除contact_order表數據
  --  oauth  qiangsw
begin

  --打開遊標
  open c_emp;
  begin
    loop
      fetch c_emp
        into order_id;
      exit when c_emp%notfound;
      --  contact_result
      insert into contact_result_201907 value
        select * from contact_result a where a.contact_order_id = order_id;

      delete from contact_result a where a.contact_order_id = order_id;
      --  contact_push_msg
      insert into contact_push_msg_201908 value
        select * from contact_push_msg where contact_order_id = order_id;

      delete from contact_push_msg c where c.contact_order_id = order_id;
      --  contact_order
      insert into contact_order_201907 value
        select * from contact_order where contact_order_id = order_id;

      delete from contact_order o where o.contact_order_id = order_id;
    end loop;
  end;
  --關閉遊標
  close c_emp;
  
  commit;

end;
------------------------------------------------------------------------------------------------------------------------------------------------------------

CREATE OR REPLACE PROCEDURE TASK_TO_HIS(i_beginTime       in varchar2, --開始時間
                                        i_endTime         in varchar2, -- 結束時間
                                        i_mkt_campaign_id in varchar2 --活動id
                                        ) as
  --年月表後綴

  cursor c_emp is
    select b.Contact_Task_Id
      from CONTACT_TASK b
     where b.mkt_campaign_id = i_mkt_campaign_id
       and b.create_date >= to_date(i_beginTime, 'yyyy/MM/dd HH24:mi:ss')
       and b.create_date <= to_date(i_endTime, 'yyyy/MM/dd hh24:mi:ss')
       and rownum < 50000;

  task_id CONTACT_TASK.CONTACT_TASK_ID%type;

  --  根據活動 id 開始時間和結束時間取 contact_task表數據 從而找到 contact_item表和contact_chl_attr_inst表與
  --  contact_task_chl表數據
  --  先插入年月表,然後刪除contact_result表和contact_push_msg表數據 最後刪除contact_order表數據
  --  oauth  qiangsw
begin

  --打開遊標
  open c_emp;
  begin
    loop
      fetch c_emp
        into task_id;
      exit when c_emp%notfound;
      --  contact_item
      insert into contact_item_201907 value
        select * from contact_item a where a.contact_task_id = task_id;

      delete from contact_item a where a.contact_task_id = task_id;

      --遊標
      declare
        cursor C_TASK_CHL IS
          select e.contact_task_chl_id
            from contact_task_chl e
           where e.contact_task_id = task_id;
        task_chl_id CONTACT_TASK_CHL.CONTACT_TASK_CHL_ID%TYPE;

      begin

        open C_TASK_CHL;

        loop
          fetch C_TASK_CHL
            into task_chl_id;
          exit when C_TASK_CHL%notfound;

          --  contact_chl_attr_inst
          insert into contact_chl_attr_inst_201907 value
            select *
              from contact_chl_attr_inst
             where CONTACT_TASK_CHL_ID = task_chl_id;

          delete from contact_chl_attr_inst c
           where c.contact_task_chl_id = task_chl_id;
          -- contact_task_chl
          insert into contact_task_chl_201907 value
            select * from contact_task_chl where contact_task_id = task_id;

          delete from contact_task_chl where contact_task_id = task_id;
        end loop;
        --關閉遊標
        close C_TASK_CHL;
      end;
      --  contact_task
      insert into contact_task_201907 value
        select * from contact_task where contact_task_id = task_id;

      delete from contact_task o where o.contact_task_id = task_id;
    end loop;
  end;
  --關閉遊標
  close c_emp;
  COMMIT;
end;
 

 

動態遊標

CREATE OR REPLACE PROCEDURE PUSHMSG_TO_HIS1(i_beginTime in varchar2, --開始時間
                                            i_endTime   in varchar2 -- 結束時間
                                            ) is
  type s_cursor is ref cursor; --申明動態遊標
  v_push_msg s_cursor; --動態遊標賦值
  pushMsgId  contact_push_msg.push_msg_id%type;
  psuhSql    varchar2(1000);
  tabelDate  varchar2(100);


 
  --  oauth  qiangsw
begin

  --打開遊標
  open v_push_msg for
    select b.push_msg_id, to_char(b.create_date, 'yyyyMM') createDate
      from contact_push_msg b
     where b.create_date > to_date(i_beginTime, 'yyyy/MM/dd HH24:mi:ss')
       and b.create_date < to_date(i_endTime, 'yyyy/MM/dd hh24:mi:ss')
       and status_cd = '1100'
       and rownum < 40000;
  --begin
  --loop
  fetch v_push_msg
    into pushMsgId, tabelDate;
  while v_push_msg%found loop
    psuhSql := 'insert into contact_push_msg_' || tabelDate ||
               ' value select * from contact_push_msg where push_msg_id =' ||
               pushMsgId;
    --EXECUTE IMMEDIATE (psuhSql);
    delete from contact_push_msg where push_msg_id = pushMsgId;
  end loop;
   commit;
  -- end loop;
  --end;
  --關閉遊標
  close v_push_msg;

end;

 

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