drop function if exists fw_core.fn_run_master_wrapper;
CREATE OR REPLACE FUNCTION fw_core.fn_run_master_wrapper(p_client_id int,p_function_id int,p_job_id int,p_step_id int)
   RETURNS void AS $$
   declare
    f record;
    stg2_status text;
    success_sum int;
    err_file_syspk int;
    TRX_status text;
    w_job_id int := p_job_id;
    w_step_id int := p_step_id;
    w_function_id int := p_function_id;
    w_client_id int := p_client_id;
   begin

--	update fw_core.fw_jobctl_runschedule_jobstep set begin_status='started',start_time=now() where job_id = w_job_id and step_id = w_step_id and latest_runschedule_flag = '1';
		
	for f in select distinct file_syspk,file_mnemonic,file_sheet_mnemonic,sheet_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 = 'generic_validation_completed'
		order by file_syspk,sheet_id	
	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('||w_client_id||','||w_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 ('||w_client_id||','||w_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('||w_client_id||','||w_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 ('||w_client_id||','||w_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('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''||f.file_sheet_mnemonic||''','||f.file_syspk||')');
	else perform  fw_core.fn_insert_db_error ( w_function_id, w_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;
	
perform archive.fn_run_block_archive(w_client_id, w_function_id);
	
--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('||w_client_id||','||w_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 ('||w_client_id||','||w_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('||w_client_id||','||w_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 ('||w_client_id||','||w_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('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_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 ('||w_client_id||','||w_function_id||','''||f.file_mnemonic||''','''|| f.file_sheet_mnemonic||''','||f.file_syspk||')');
	else perform  fw_core.fn_insert_db_error ( w_client_id, w_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;


perform transactional.fn_run_updates(w_client_id, w_function_id);

-- 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_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_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_jobctl_runschedule_jobstep table.
SELECT SUM (case WHEN end_status = 'success' THEN 1
	       	 ELSE 0 END) AS "Success Sum" FROM fw_core.fw_jobctl_file_runschedule where latest_runschedule_flag = '1' INTO success_sum;
if (success_sum > 0 or success_sum is null) then
update fw_core.fw_jobctl_runschedule_jobstep set end_status_note = 'trx_completed', end_status='success', end_time=now() where job_id = w_job_id and step_id = w_step_id and latest_runschedule_flag = '1';
else 
update fw_core.fw_jobctl_runschedule_jobstep set end_status_note = 'trx_failed', end_status='success', end_time=now() where job_id = w_job_id and step_id = w_step_id and latest_runschedule_flag = '1';
end if;



end;
$$  LANGUAGE plpgsql;