drop function if exists mmt_staging2.fn_DBSTD_RUN;
CREATE OR REPLACE FUNCTION mmt_staging2.fn_DBSTD_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 = 'DBSTD' order by file_syspk
    loop 
    select f.file_syspk into w_file_syspk;
    perform mmt_staging2.fn_dbstd_test_block(1,2,'DBSTD','DBSTD_TEST',w_file_syspk);
    select e.status from mmt_staging2.mmt_DB_Run_Status e where file_syspk = w_file_syspk and sheet_mnemonic ='DBSTD_TEST' into status;
    if status = 'success' then
		perform mmt_ods.fn_dbstd_test_ods(w_file_syspk);
	end if;
    end loop;
end;
$$  LANGUAGE plpgsql;  

--select mmt_staging2.fn_DBSTD_RUN()