Tuesday, 30 July 2013

How to create a history table

CREATE TABLE TABLE1
(
  ASSIGNMENT_ID     NUMBER                      NOT NULL,
  CREATION_DATE     DATE                        NOT NULL,
  CREATED_BY        VARCHAR2(30 BYTE)           NOT NULL,
  LAST_UPDATE_DATE  DATE                        NOT NULL,
  LAST_UPDATED_BY   VARCHAR2(30 BYTE)           NOT NULL,
  EMPLOYEE_ID       NUMBER                      NOT NULL,
  START_DATE        DATE                        NOT NULL,
  END_DATE          DATE,
  PRIMARY_FLAG      VARCHAR2(1 BYTE)            NOT NULL,
  DESCRIPTION       VARCHAR2(240 BYTE),
  PERF_MGR_ID       NUMBER,
  PERF_MRG_START_DATE   DATE,
  PERF_MRG_END_DATE     DATE,
  WORK_LOCATION     VARCHAR2(240 BYTE),
  JOB_TITLE         VARCHAR2(30 BYTE),
  ORG_ID            NUMBER                      NOT NULL,
  HR_MGR_ID         NUMBER,
  WORK_SHIFT        VARCHAR2(30 BYTE),
  PROJECT_MGR_ID    NUMBER,
  PROJECT_MRG_START_DATE    DATE,
  PROJECT_MRG_END_DATE      DATE
)

=======================================================

CREATE TABLE HISTORYTABLE
(
  ASSIGNMENT_ID     NUMBER                      NOT NULL,
  CREATION_DATE     DATE                        NOT NULL,
  CREATED_BY        VARCHAR2(30 BYTE)           NOT NULL,
  LAST_UPDATE_DATE  DATE                        NOT NULL,
  LAST_UPDATED_BY   VARCHAR2(30 BYTE)           NOT NULL,
  EMPLOYEE_ID       NUMBER                      NOT NULL,
  START_DATE        DATE                        NOT NULL,
  END_DATE          DATE,
  PRIMARY_FLAG      VARCHAR2(1 BYTE)            NOT NULL,
  DESCRIPTION       VARCHAR2(240 BYTE),
  PERF_MGR_ID       NUMBER,
  PERF_MRG_START_DATE   DATE,
  PERF_MRG_END_DATE     DATE,
  WORK_LOCATION     VARCHAR2(240 BYTE),
  JOB_TITLE         VARCHAR2(30 BYTE),
  HR_MGR_ID         NUMBER,
  WORK_SHIFT        VARCHAR2(30 BYTE),
  PROJECT_MGR_ID    NUMBER,
  PROJECT_MRG_START_DATE    DATE,
  PROJECT_MRG_END_DATE      DATE,
  ORG_ID            NUMBER                      NOT NULL,
  REVISION_NUM        NUMBER
)





===================================================

CREATE OR REPLACE TRIGGER <TRIGGER_NAME>
  AFTER UPDATE ON TABLE1
  FOR EACH ROW
DECLARE

  l_revision_num    NUMBER;

BEGIN

  SELECT MAX(revision_num)
    INTO l_revision_num
    FROM HISTORYTABLE
   WHERE ASSIGNMENT_ID = :new.ASSIGNMENT_ID;


   IF nvl(l_revision_num, 0) = 0 then
      l_revision_num := 1;
   ELSE
      l_revision_num := l_revision_num + 1;
   END IF;

   INSERT INTO OGS_HR_ASSIGNMENTS_H_ALL (
                                    REVISION_NUM
                                  , ASSIGNMENT_ID
                                  , CREATION_DATE
                                  , CREATED_BY
                                  , LAST_UPDATE_DATE
                                  , LAST_UPDATED_BY
                                  , EMPLOYEE_ID
                                  , START_DATE
                                  , END_DATE
                                  , PRIMARY_FLAG
                                  , DESCRIPTION
                                  , PERF_MGR_ID
                                  , PERF_MRG_START_DATE
                                  , PERF_MRG_END_DATE
                                  , WORK_LOCATION
                                  , JOB_TITLE
                                  , HR_MGR_ID
                                  , PROJECT_MGR_ID
                                  , PROJECT_MRG_START_DATE
                                  , PROJECT_MRG_END_DATE
                                  , ORG_ID
                                )
                     VALUES  (
                                    l_revision_num
                                  , :old.assignment_id
                                  , :old.creation_date
                                  , :old.created_by
                                  , :old.last_update_date
                                  , :old.last_updated_by
                                  , :old.employee_id
                                  , :old.start_date
                                  , :old.end_date
                                  , :old.primary_flag
                                  , :old.description
                                  , :old.perf_mgr_id
                                  , :old.perf_mrg_start_date
                                  , :old.perf_mrg_end_date
                                  , :old.work_location
                                  , :old.job_title
                                  , :old.hr_mgr_id
                                  , :old.project_mgr_id
                                  , :old.project_mrg_start_date
                                  , :old.project_mrg_end_date
                                  , :old.org_id                            
                             );
EXCEPTION
  WHEN OTHERS
  THEN
    raise_application_error (-20100, SQLERRM);
END OGS_HR_ASSIGNMENTS_ALL_AU;
/

No comments:

Post a Comment