create table HRIS_TERMINATED_EMPLOYEES(
EMPLOYEE_ID NUMBER(7),
FULL_NAME VARCHAR2(255)
);


INSERT into HRIS_TERMINATED_EMPLOYEES values(EMPLOYEE_ID, FULL_NAME);


DECLARE
P_SERVICE_EVENT_TYPE_ID NUMBER :=2;
P_TO_SERVICE_TYPE_ID NUMBER :=2;
BEGIN
FOR employee IN
(SELECT TE.EMPLOYEE_ID AS EMPLOYEE_ID,
E.BRANCH_ID AS BRANCH_ID,
E.DEPARTMENT_ID AS DEPARTMENT_ID,
E.DESIGNATION_ID AS DESIGNATION_ID,
E.POSITION_ID AS POSITION_ID,
E.COMPANY_ID AS COMPANY_ID,
E.SALARY AS SALARY
FROM HRIS_TERMINATED_EMPLOYEES TE
LEFT JOIN HRIS_EMPLOYEES E
ON (TE.EMPLOYEE_ID = E.EMPLOYEE_ID)
)
LOOP
INSERT
INTO HRIS_JOB_HISTORY
(
JOB_HISTORY_ID,
EMPLOYEE_ID,
START_DATE,
SERVICE_EVENT_TYPE_ID,
TO_BRANCH_ID,
TO_DEPARTMENT_ID,
TO_DESIGNATION_ID,
TO_POSITION_ID,
TO_SERVICE_TYPE_ID,
STATUS,
CREATED_DT,
TO_COMPANY_ID,
TO_SALARY,
RETIRED_FLAG,
DISABLED_FLAG,
EVENT_DATE
)
VALUES
(
(SELECT MAX(JOB_HISTORY_ID)+1 FROM HRIS_JOB_HISTORY),
employee.EMPLOYEE_ID,
TRUNC(sysdate),
P_SERVICE_EVENT_TYPE_ID,
employee.BRANCH_ID,
employee.DEPARTMENT_ID,
employee.DESIGNATION_ID,
employee.POSITION_ID,
P_TO_SERVICE_TYPE_ID,
'E',
'01-SEP-19',
employee.COMPANY_ID,
employee.SALARY,
'Y',
'Y',
'01-SEP-19'
);
END LOOP;

END;