上次使用oracle 11g更新日期

时间:2016-02-27 16:26:59

标签: sql oracle11g

目前我有一个进程,每2小时执行一次,基于oracle 11g表每2小时更新一次。

update_date >= sysdate -2/24  

无论如何,我可以每2小时运行一次工作并捕获自上次更新运行以来的更新。

由于

1 个答案:

答案 0 :(得分:0)

以下是如何做到这一点的简化示例:

1)设置数据结构并初始化作业的状态

CREATE TABLE ak_job_state(last_date DATE);
INSERT INTO ak_job_state VALUES (SYSDATE-2/24);

CREATE TABLE ak_example(c_name VARCHAR2(100), last_updated DATE);

2)设置JOB程序:

CREATE OR REPLACE PROCEDURE check_for_updated IS
  v_last_date DATE;
  v_new_date  DATE;
BEGIN
  SELECT last_date INTO v_last_date FROM ak_job_state FOR UPDATE NOWAIT; --get the date from storage (meanwhile serialize the access)

  v_new_date := SYSDATE; --make new date state

  FOR x IN (SELECT * FROM ak_example WHERE last_updated > v_last_date) LOOP

    --Do your stuff;
    NULL;
  END LOOP;

  UPDATE ak_job_state SET last_date = v_new_date; --Save the state
  COMMIT;
END;
/

3)并安排JOB,有关Oracle文档中调度程序的更多详细信息:https://docs.oracle.com/cd/B28359_01/server.111/b28310/schedadmin006.htm

BEGIN
  dbms_scheduler.create_job(job_name        => 'check_for_updated_job'
                           ,job_type        => 'PLSQL_BLOCK'
                           ,job_action      => 'BEGIN check_for_updated; END;'
                           ,start_date      => SYSDATE
                           ,repeat_interval => 'FREQ=HOURLY;INTERVAL=2'
                           ,enabled         => TRUE
                           ,comments        => 'Update every 2 hours');
END;
/