drop function if exists mmt_staging2.fn_BUDNI_RUN;
CREATE OR REPLACE FUNCTION mmt_staging2.fn_BUDNI_RUN()
   RETURNS void AS $$
   declare
   	f record;
    w_file_syspk int;
    status text;
   begin
   for f in select distinct a.file_syspk 
		from mmt_staging1.mmt_staging_generic_table a
		where file_format = 'BUDNI'  order by file_syspk
    loop 
    select f.file_syspk into w_file_syspk;
    perform mmt_staging2.fn_BUDNI_ARC_Block(20,1,'BUDNI','BUDNI_ARC',w_file_syspk);
    select e.status from mmt_staging2.mmt_DB_Run_Status e where file_syspk = w_file_syspk and sheet_mnemonic ='BUDNI_ARC' into status;
    if status = 'success' then
		perform mmt_staging2.fn_BUDNI_ARC_ODS(w_file_syspk);
	end if;
	perform mmt_staging2.fn_BUDNI_BRK_Block (20,1,'BUDNI','BUDNI_BRK',w_file_syspk);
	select e.status from mmt_staging2.mmt_DB_Run_Status e where file_syspk = w_file_syspk and sheet_mnemonic ='BUDNI_BRK' into status;
	if status = 'success' then
	perform mmt_staging2.fn_BUDNI_BRK_ODS(w_file_syspk);
    end if;
    perform mmt_staging2.fn_BUDNI_DBP_Block(20,1,'BUDNI','BUDNI_DBP',w_file_syspk);
    select e.status from mmt_staging2.mmt_DB_Run_Status e where file_syspk = w_file_syspk and sheet_mnemonic ='BUDNI_DBP' into status;
    if status = 'success' then
		perform mmt_staging2.fn_BUDNI_DBP_ODS(w_file_syspk);
	end if;
    perform mmt_staging2.fn_BUDNI_FLD_Block(20,1,'BUDNI','BUDNI_FLD',w_file_syspk);
    select e.status from mmt_staging2.mmt_DB_Run_Status e where file_syspk = w_file_syspk and sheet_mnemonic ='BUDNI_FLD' into status;
    if status = 'success' then
		perform mmt_staging2.fn_BUDNI_FLD_ODS(w_file_syspk);
	end if;
     perform mmt_staging2.fn_BUDNI_HDL_Block(20,1,'BUDNI','BUDNI_HDL',w_file_syspk);
    select e.status from mmt_staging2.mmt_DB_Run_Status e where file_syspk = w_file_syspk and sheet_mnemonic ='BUDNI_HDL' into status;
    if status = 'success' then
		perform mmt_staging2.fn_BUDNI_HDL_ODS(w_file_syspk);
	end if;
    perform mmt_staging2.fn_BUDNI_HLG_Block(20,1,'BUDNI','BUDNI_HLG',w_file_syspk);
    select e.status from mmt_staging2.mmt_DB_Run_Status e where file_syspk = w_file_syspk and sheet_mnemonic ='BUDNI_HLG' into status;
    if status = 'success' then
		perform mmt_staging2.fn_BUDNI_HLG_ODS(w_file_syspk);
	end if;
    perform mmt_staging2.fn_BUDNI_LCG_Block(20,1,'BUDNI','BUDNI_LCG',w_file_syspk);
    select e.status from mmt_staging2.mmt_DB_Run_Status e where file_syspk = w_file_syspk and sheet_mnemonic ='BUDNI_LCG' into status;
    if status = 'success' then
		perform mmt_staging2.fn_BUDNI_LCG_ODS(w_file_syspk);
	end if;
    perform mmt_staging2.fn_BUDNI_NMT_Block(20,1,'BUDNI','BUDNI_NMT',w_file_syspk);
    select e.status from mmt_staging2.mmt_DB_Run_Status e where file_syspk = w_file_syspk and sheet_mnemonic ='BUDNI_NMT' into status;
    if status = 'success' then
		perform mmt_staging2.fn_BUDNI_NMT_ODS(w_file_syspk);
	end if;
    perform mmt_staging2.fn_BUDNI_PTO_Block(20,1,'BUDNI','BUDNI_PTO',w_file_syspk);
    select e.status from mmt_staging2.mmt_DB_Run_Status e where file_syspk = w_file_syspk and sheet_mnemonic ='BUDNI_PTO' into status;
    if status = 'success' then
		perform mmt_staging2.fn_BUDNI_PTO_ODS(w_file_syspk);
	end if;
    perform mmt_staging2.fn_BUDNI_VMT_Block(20,1,'BUDNI','BUDNI_VMT',w_file_syspk);
    select e.status from mmt_staging2.mmt_DB_Run_Status e where file_syspk = w_file_syspk and sheet_mnemonic ='BUDNI_VMT' into status;
    if status = 'success' then
		perform mmt_staging2.fn_BUDNI_VMT_ODS(w_file_syspk);
	end if;
    end loop;
   
end;
$$  LANGUAGE plpgsql;  

select mmt_staging2.fn_BUDNI_RUN();


delete from mmt_staging2.mmt_DB_Run_Status where file_format = 'BUDNI';
select count(distinct file_syspk) from mmt_staging2.mmt_DB_Run_Status
select file_syspk,sheet_mnemonic from mmt_staging2.mmt_DB_Run_Status where status = 'error';
select * from mmt_staging2.mmt_DB_Run_Status where file_format = 'BUDNI' and status = 'error';
select distinct file_syspk from mmt_staging2.mmt_DB_Run_Status where file_format = 'BUDNI';




