536: ,p_org_hist_tbl IN OUT NOCOPY csi_diagnostics_pkg.T_NUM
537: ) IS
538: BEGIN
539: FOR i in p_org_tbl.FIRST .. p_org_tbl.LAST LOOP
540: select CSI_I_ORG_ASSIGNMENTS_H_S.nextval
541: into p_org_hist_tbl(i) from dual;
542: --
543: p_org_units_rec_tab.instance_ou_id(i) := p_org_tbl(i).instance_ou_id;
544: p_org_units_rec_tab.instance_id(i) := p_org_tbl(i).instance_id;
959: x_ou_tbl(l_ind).attribute14 := p_ou_h_tbl(l_ind).attribute14;
960: x_ou_tbl(l_ind).attribute15 := p_ou_h_tbl(l_ind).attribute15;
961: SELECT object_version_number
962: INTO x_ou_tbl(l_ind).object_version_number
963: FROM csi_i_org_assignments
964: WHERE instance_ou_id = p_ou_h_tbl(l_ind).instance_ou_id;
965:
966: END LOOP;
967: END IF;
2229: --
2230: -- Insert into History
2231: log('Before Inserting into Org Assignments history');
2232: FORALL i in 1 .. l_org_units_rec_tab.instance_ou_id.count
2233: INSERT INTO CSI_I_ORG_ASSIGNMENTS_H(
2234: INSTANCE_OU_HISTORY_ID,
2235: INSTANCE_OU_ID,
2236: TRANSACTION_ID,
2237: NEW_OPERATING_UNIT_ID,
15898: --
15899: PROCEDURE Delete_Dup_Org_Assignments IS
15900: cursor csi_dup_cur is
15901: select instance_id,relationship_type_code
15902: from csi_i_org_assignments
15903: group by instance_id,relationship_type_code
15904: having count(*) > 1;
15905: --
15906: cursor csi_org_cur(p_instance_id in number,p_rel_type_code in varchar2) is
15904: having count(*) > 1;
15905: --
15906: cursor csi_org_cur(p_instance_id in number,p_rel_type_code in varchar2) is
15907: select *
15908: from csi_i_org_assignments
15909: where instance_id = p_instance_id
15910: and relationship_type_code = p_rel_type_code
15911: order by creation_date asc;
15912: --
15910: and relationship_type_code = p_rel_type_code
15911: order by creation_date asc;
15912: --
15913: cursor csi_org_hist_cur(p_instance_id in number,p_rel_type_code in varchar2) is
15914: select coah.* from csi_i_org_assignments_h coah,
15915: csi_i_org_assignments coa
15916: where coa.instance_id = p_instance_id
15917: and coa.relationship_type_code = p_rel_type_code
15918: and coah.instance_ou_id = coa.instance_ou_id
15911: order by creation_date asc;
15912: --
15913: cursor csi_org_hist_cur(p_instance_id in number,p_rel_type_code in varchar2) is
15914: select coah.* from csi_i_org_assignments_h coah,
15915: csi_i_org_assignments coa
15916: where coa.instance_id = p_instance_id
15917: and coa.relationship_type_code = p_rel_type_code
15918: and coah.instance_ou_id = coa.instance_ou_id
15919: order by coah.transaction_id,coah.last_update_date asc;
15926: l_row_count NUMBER;
15927: l_del_count NUMBER := 0;
15928: TYPE NumList IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
15929: l_del_tbl NumList;
15930: TYPE ORG_HIST_TBL IS TABLE OF CSI_I_ORG_ASSIGNMENTS_H%ROWTYPE INDEX BY BINARY_INTEGER;
15931: l_org_hist_tbl ORG_HIST_TBL;
15932: l_hist_count NUMBER;
15933: v_ret_ou_id NUMBER;
15934: l_prev_rec csi_org_hist_cur%ROWTYPE;
15944: v_max_ou_id := -9999;
15945: Begin
15946: select instance_ou_id
15947: into v_max_ou_id
15948: from csi_i_org_assignments
15949: where instance_id = dup_rec.instance_id
15950: and relationship_type_code = dup_rec.relationship_type_code
15951: and creation_date = ( select max(creation_date)
15952: from csi_i_org_assignments
15948: from csi_i_org_assignments
15949: where instance_id = dup_rec.instance_id
15950: and relationship_type_code = dup_rec.relationship_type_code
15951: and creation_date = ( select max(creation_date)
15952: from csi_i_org_assignments
15953: where instance_id = dup_rec.instance_id
15954: and relationship_type_code = dup_rec.relationship_type_code
15955: and nvl(active_end_date,(sysdate+1)) > sysdate)
15956: and nvl(active_end_date,(sysdate+1)) > sysdate
15958: Exception
15959: when no_data_found then
15960: select max(instance_ou_id)
15961: into v_max_ou_id
15962: from csi_i_org_assignments
15963: where instance_id = dup_rec.instance_id
15964: and relationship_type_code = dup_rec.relationship_type_code;
15965: End;
15966: --
15974: FOR org_rec in csi_org_cur(dup_rec.instance_id,dup_rec.relationship_type_code) LOOP
15975: l_row_count := l_row_count + 1;
15976: IF l_row_count = 1 THEN
15977: v_ret_ou_id := org_rec.instance_ou_id;
15978: UPDATE CSI_I_ORG_ASSIGNMENTS
15979: set (operating_unit_id,active_end_date,context,attribute1,attribute2,attribute3,
15980: attribute4,attribute5,attribute6,attribute7,attribute8,attribute9,attribute10,
15981: attribute11,attribute12,attribute13,attribute14,attribute15,last_update_date) =
15982: (select operating_unit_id,active_end_date,context,attribute1,attribute2,attribute3,
15981: attribute11,attribute12,attribute13,attribute14,attribute15,last_update_date) =
15982: (select operating_unit_id,active_end_date,context,attribute1,attribute2,attribute3,
15983: attribute4,attribute5,attribute6,attribute7,attribute8,attribute9,attribute10,
15984: attribute11,attribute12,attribute13,attribute14,attribute15,sysdate
15985: from csi_i_org_assignments
15986: where instance_ou_id = v_max_ou_id)
15987: where instance_ou_id = org_rec.instance_ou_id;
15988: ELSE
15989: l_del_count := l_del_count + 1;
16137: END IF;
16138: --
16139: ELSE -- Txn id not same
16140: IF l_flag = 'Y' THEN
16141: update csi_i_org_assignments_h
16142: set old_operating_unit_id = l_prev_rec.old_operating_unit_id,
16143: new_operating_unit_id = l_prev_rec.new_operating_unit_id,
16144: old_relationship_type_code = l_prev_rec.old_relationship_type_code,
16145: new_relationship_type_code = l_prev_rec.new_relationship_type_code,
16182: last_update_date = sysdate
16183: where instance_ou_history_id = l_prev_rec.instance_ou_history_id;
16184: --
16185: FORALL i in 1..l_del_ou_hist_tbl.count
16186: delete from csi_i_org_assignments_h
16187: where instance_ou_history_id = l_del_ou_hist_tbl(i);
16188: --
16189: l_flag := 'N';
16190: l_del_ou_hist_tbl.DELETE;
16194: END IF;
16195: END LOOP;
16196: -- Update the instance_ou_id with the one that is retained
16197: IF l_flag = 'Y' THEN -- Just in case the last record in the loop matches with prev txn
16198: update csi_i_org_assignments_h
16199: set old_operating_unit_id = l_prev_rec.old_operating_unit_id,
16200: new_operating_unit_id = l_prev_rec.new_operating_unit_id,
16201: old_relationship_type_code = l_prev_rec.old_relationship_type_code,
16202: new_relationship_type_code = l_prev_rec.new_relationship_type_code,
16239: last_update_date = sysdate
16240: where instance_ou_history_id = l_prev_rec.instance_ou_history_id;
16241: --
16242: FORALL i in 1..l_del_ou_hist_tbl.count
16243: delete from csi_i_org_assignments_h
16244: where instance_ou_history_id = l_del_ou_hist_tbl(i);
16245: --
16246: l_flag := 'N';
16247: l_del_ou_hist_tbl.DELETE;
16247: l_del_ou_hist_tbl.DELETE;
16248: END IF;
16249: --
16250: FOR x in l_org_hist_tbl.FIRST .. l_org_hist_tbl.LAST LOOP
16251: update csi_i_org_assignments_h
16252: set instance_ou_id = v_ret_ou_id,
16253: last_update_date = sysdate
16254: where instance_ou_history_id = l_org_hist_tbl(x).instance_ou_history_id;
16255: END LOOP;
16256: END IF;
16257: --
16258: -- Delete the Duplicate Org Assignments
16259: FORALL x in l_del_tbl.FIRST .. l_del_tbl.LAST
16260: DELETE FROM CSI_I_ORG_ASSIGNMENTS
16261: where instance_ou_id = l_del_tbl(x);
16262: commit;
16263: Exception
16264: when Process_next then