-- FUNCTION: public.fupdatedone(bigint)

-- DROP FUNCTION IF EXISTS public.fupdatedone(bigint);

CREATE OR REPLACE FUNCTION public.fupdatedone(
	par_importid bigint)
    RETURNS bigint
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
AS $BODY$
   DECLARE
      importid bigint;
   BEGIN
      SELECT DI.dataimportid
	FROM internal.dataimport DI
	WHERE DI.SourceDataImportID = par_importid
		AND DI.DataImportStatus = 'Success'
	ORDER BY DI.CompleteTime DESC nulls last LIMIT 1 INTO importid;

	IF importid is null
	then 
	select 0 into importid;
	END IF;
      RETURN importid;
   END;
   
$BODY$;

ALTER FUNCTION public.fupdatedone(bigint)
    OWNER TO postgres;
