drop function if exists staging2.fn_run_master_wrapper;
CREATE OR REPLACE FUNCTION staging2.fn_run_master_wrapper()
   RETURNS void AS $$
   declare
   	f record;
    stg2_status text;
  	error_sum int;
    err_file_syspk int;
   	TRX_status text;
   begin

	
	update fw_core.fw_jobctl_runschedule_jobstep set begin_status='Started',start_time=now() where job_id = 2 and step_id = 1 and latest_runschedule_flag = '1';
	
	for f in select distinct file_syspk,file_mnemonic,file_sheet_mnemonic,client_id,function_id 
		from fw_core.fw_jobctl_file_sheet_runschedule where file_sheet_mnemonic !=''
		and latest_runschedule_flag = '1' and end_status != 'error' and end_status_note = 'stg1_completed'--and file_mnemonic  not in ('DBOECD','IHTSLL','IHT')
		order by file_syspk
	loop
	
	if err_file_syspk = f.file_syspk then
		--incase of file stg2/trx fail then dont run script for those files and rollback previous change if any.
  		--rollback for that file syspk needs to be added
		--perform staging2.run_trx_rollback(f.file_syspk)
		continue;
	else
	--run staging 2 scripts depending on sheet mnemonic
    if f.file_sheet_mnemonic = 'FTDRY_TRS'  then
   		perform fw_core.fn_get_function_exception('staging2.fn_FTDRY_TRS_Block('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');    
   	elsif f.file_sheet_mnemonic = 'FTDRY_SUM'  then
    	perform fw_core.fn_get_function_exception('staging2.fn_FTDRY_SUM_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
	elsif f.file_sheet_mnemonic = 'FTWET_TRS'  then
   		perform fw_core.fn_get_function_exception('staging2.fn_ftwet_trs_block('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'FTWET_SUM'  then
    	perform fw_core.fn_get_function_exception('staging2.fn_FTWET_SUM_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
	elsif f.file_sheet_mnemonic = 'FTHLG_TRS'  then
   		perform fw_core.fn_get_function_exception('staging2.fn_FTHLG_TRS_Block('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'FTHLG_SUM'  then
    	perform fw_core.fn_get_function_exception('staging2.fn_FTHLG_SUM_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'DBSTD_TEST'  then
    	perform fw_core.fn_get_function_exception('staging2.fn_dbstd_test_block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'DBOECD_TEST'  then
    	perform fw_core.fn_get_function_exception('staging2.fn_dboecd_test_block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic in ('PTOBEN_PRFN','PTOBEN_PRFE','PTOBEN_PRFB','PTOSTD_PRF')  then
    	perform fw_core.fn_get_function_exception('staging2.fn_PTO_PRF_block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic in ('PTOBEN_MPM','PTOSTD_MPM')  then
    	perform fw_core.fn_get_function_exception('staging2.fn_PTO_MPM_block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic in ('PTOBEN_GVG','PTOSTD_GVG')  then
    	perform fw_core.fn_get_function_exception('staging2.fn_PTO_GVG_block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'BUDNI_BRK' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_BRK_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'BUDNI_FLD' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_FLD_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'BUDNI_HDL' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_HDL_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'BUDNI_NMT' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_NMT_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'BUDNI_HLG' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_HLG_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'BUDNI_PTO' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_PTO_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'BUDNI_VMT' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_VMT_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'BUDNI_ARC' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_ARC_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'BUDNI_DBP' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_DBP_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'BUDNI_LCG' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_LCG_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'IHTCGM_CGM' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTCGM_CGM_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'IHTEMT_STD' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTEMT_STD_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'IHTEMT_EMT' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTEMT_EMT_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'IHTHAM_HAM' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTHAM_HAM_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'IHTHLS_HLS' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTHLS_HLS_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'IHTNST_NST' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTNST_NST_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'IHTSLL_SLL' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTSLL_SLL_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'IHTBT30_BT30' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTBT30_BT30_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'IHTBT50_BT50' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTBT50_BT50_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif f.file_sheet_mnemonic = 'IHTBTD_BTD' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTBTD_BTD_Block ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
	else perform  fw_core.fn_insert_db_error ( f.function_id, f.function_id, 1001, 'Compegence', f.file_syspk ,f.file_mnemonic,f.file_sheet_mnemonic ,null,'stg2 wrapper', 'unknown block', null, 'no script found', 'Script not present for the particlar sheet mnemonic', null, null,'error');
end if;
	-- check if staging 2 script ran correctly and update fw_jobctl_file_sheet_runschedule table.
    select e.status from fw_core.db_run_status e where file_syspk = f.file_syspk and file_sheet_mnemonic =f.file_sheet_mnemonic and staging_type = 'stg2' order by error_timestamp desc into stg2_status;
		if stg2_status = 'success' then
   			update fw_core.fw_jobctl_file_sheet_runschedule set end_status_note = 'stg2_completed', end_status='success',staging_type = 'stg2',end_time = (select e.error_timestamp from fw_core.db_run_status e where e.file_syspk=f.file_syspk and e.file_sheet_mnemonic =f.file_sheet_mnemonic and e.staging_type = 'stg2' order by e.error_timestamp desc limit 1)
			 where file_syspk=f.file_syspk and file_sheet_mnemonic =f.file_sheet_mnemonic;
		else 
			update fw_core.fw_jobctl_file_sheet_runschedule set end_status_note = 'stg2_failed', end_status='error',staging_type = 'stg2',end_time = (select e.error_timestamp from fw_core.db_run_status e where e.file_syspk=f.file_syspk and e.file_sheet_mnemonic =f.file_sheet_mnemonic and e.staging_type = 'stg2' order by e.error_timestamp desc limit 1)
			 			where file_syspk=f.file_syspk and file_sheet_mnemonic =f.file_sheet_mnemonic;
		--if error occured store in err_file_syspk variable and go to next file in loop
			err_file_syspk := f.file_syspk;
			continue;
		end if;
	
	
--run transactional scripts depending on sheet mnemonic	
	if  f.file_sheet_mnemonic = 'FTDRY_TRS'  then
   		perform fw_core.fn_get_function_exception('staging2.fn_FTDRY_TRS_TRX('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');    
   	elsif  f.file_sheet_mnemonic = 'FTDRY_SUM'  then
    	perform fw_core.fn_get_function_exception('staging2.fn_FTDRY_SUM_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
	elsif  f.file_sheet_mnemonic = 'FTWET_TRS'  then
   		perform fw_core.fn_get_function_exception('staging2.fn_ftwet_trs_TRX('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'FTWET_SUM'  then
    	perform fw_core.fn_get_function_exception('staging2.fn_FTWET_SUM_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
	elsif  f.file_sheet_mnemonic = 'FTHLG_TRS'  then
   		perform fw_core.fn_get_function_exception('staging2.fn_FTHLG_TRS_TRX('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'FTHLG_SUM'  then
    	perform fw_core.fn_get_function_exception('staging2.fn_FTHLG_SUM_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'DBSTD_TEST'  then
    	perform fw_core.fn_get_function_exception('staging2.fn_dbstd_test_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'DBOECD_TEST'  then
    	perform fw_core.fn_get_function_exception('staging2.fn_dboecd_test_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic in ('PTOBEN_PRFN','PTOBEN_PRFE','PTOBEN_PRFB','PTOSTD_PRF')  then
    	perform fw_core.fn_get_function_exception('staging2.fn_PTO_PRF_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic in ('PTOBEN_MPM','PTOSTD_MPM')  then
    	perform fw_core.fn_get_function_exception('staging2.fn_PTO_MPM_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic in ('PTOBEN_GVG','PTOSTD_GVG')  then
    	perform fw_core.fn_get_function_exception('staging2.fn_PTO_GVG_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'BUDNI_BRK' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_BRK_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'BUDNI_FLD' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_FLD_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'BUDNI_HDL' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_HDL_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'BUDNI_NMT' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_NMT_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'BUDNI_HLG' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_HLG_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'BUDNI_PTO' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_PTO_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'BUDNI_VMT' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_VMT_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'BUDNI_ARC' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_ARC_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'BUDNI_DBP' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_DBP_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'BUDNI_LCG' then
    	perform fw_core.fn_get_function_exception('staging2.fn_BUDNI_LCG_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'IHTCGM_CGM' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTCGM_CGM_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'IHTEMT_STD' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTEMT_STD_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'IHTEMT_EMT' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTEMT_EMT_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'IHTHAM_HAM' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTHAM_HAM_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'IHTHLS_HLS' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTHLS_HLS_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'IHTNST_NST' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTNST_NST_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'IHTSLL_SLL' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTSLL_SLL_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'IHTBT30_BT30' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTBT30_BT30_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'IHTBT50_BT50' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTBT50_BT50_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
    elsif  f.file_sheet_mnemonic = 'IHTBTD_BTD' then
    	perform fw_core.fn_get_function_exception('staging2.fn_IHTBTD_BTD_TRX ('||f.client_id||','||f.function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
	else perform  fw_core.fn_insert_db_error ( f.function_id, f.function_id, 1001, 'Compegence', f.file_syspk ,f.file_mnemonic, f.file_sheet_mnemonic ,null,'TRX wrapper', 'unknown TRX', null, 'no script found', null, null, null,'error');
end if;
	-- check if transactional script ran correctly and update fw_jobctl_file_sheet_runschedule table.
	select e.status from fw_core.db_run_status e where file_syspk = f.file_syspk and file_sheet_mnemonic =f.file_sheet_mnemonic and staging_type = 'trx' order by error_timestamp desc into TRX_status;
		if TRX_status = 'success' then
   			update fw_core.fw_jobctl_file_sheet_runschedule set end_status_note = 'TRX_completed', end_status='success',staging_type = 'trx',end_time = now()
--(select e.error_timestamp from fw_core.db_run_status e where e.file_syspk=f.file_syspk and e.file_sheet_mnemonic =f.file_sheet_mnemonic and e.staging_type = 'trx' order by e.error_timestamp desc limit 1)
			 where file_syspk=f.file_syspk and file_sheet_mnemonic =f.file_sheet_mnemonic;
		else 
			update fw_core.fw_jobctl_file_sheet_runschedule set end_status_note = 'TRX_failed', end_status='error',staging_type = 'trx',end_time = now()
--(select e.error_timestamp from fw_core.db_run_status e where e.file_syspk=f.file_syspk and e.file_sheet_mnemonic =f.file_sheet_mnemonic and e.staging_type = 'trx' order by e.error_timestamp desc limit 1)
			where file_syspk=f.file_syspk and file_sheet_mnemonic =f.file_sheet_mnemonic;
		--if error occured store in err_file_syspk variable and go to next file in loop
			err_file_syspk := f.file_syspk;
			continue;
		end if;
end if;
end loop;

-- update fw_jobctl_file_runschedule table.
update fw_core.fw_jobctl_file_runschedule set end_status_note = 'stg2_failed', end_status='error',staging_type = 'stg2',end_time=now() from fw_core.fw_jobctl_file_sheet_runschedule a
where a.latest_runschedule_flag = '1' and a.end_status = 'error' and a.end_status_note = 'stg2_failed' and fw_core.fw_jobctl_file_runschedule.file_syspk = a.file_syspk;

update fw_core.fw_jobctl_file_runschedule set end_status_note = 'TRX_failed', end_status='error',staging_type = 'trx' ,end_time=now() from fw_core.fw_jobctl_file_sheet_runschedule a
where a.latest_runschedule_flag = '1' and a.end_status = 'error' and a.end_status_note = 'TRX_failed' and fw_core.fw_jobctl_file_runschedule.file_syspk = a.file_syspk;

update fw_core.fw_jobctl_file_runschedule set end_status_note = 'TRX_completed', end_status='success',staging_type = 'trx',end_time=now() from fw_core.fw_jobctl_file_sheet_runschedule a
where a.latest_runschedule_flag = '1' and a.end_status = 'success' and a.end_status_note ='TRX_completed' and fw_core.fw_jobctl_file_runschedule.file_syspk = a.file_syspk;

-- update fw_jobctl_runschedule_jobstep table.
SELECT SUM (case WHEN end_status = 'success' THEN 0
	       	 ELSE 1 END) AS "Error Sum" FROM fw_core.fw_jobctl_file_runschedule where latest_runschedule_flag = '1' INTO error_sum;
if error_sum = 0 then
update fw_core.fw_jobctl_runschedule_jobstep set end_status_note = 'TRX_completed', end_status='success', end_time=now() where job_id = 2 and step_id = 1 and latest_runschedule_flag = '1';
else 
update fw_core.fw_jobctl_runschedule_jobstep set end_status_note = 'TRX_failed', end_status='error', end_time=now() where job_id = 2 and step_id = 1 and latest_runschedule_flag = '1';
end if;


perform (select staging2.run_trx_rollback());

end;
$$  LANGUAGE plpgsql;