drop procedure if exists dbo.sp_CleanFrlloProcess;
drop procedure if exists public.sp_CleanFrlloProcess;
-- =============================================
-- Author:      Vitacore (с) 
-- Create date: 2024 11 25
-- Description: Выгрузка данных в ФРЛЛО
-- =============================================
create or replace procedure rds.sp_CleanFrlloProcess
(
	"ExportId" uuid default null::uuid
)
language plpgsql
as
$$
	declare _Num int := 1000;
begin

	create temp table TMP_OBJ 
	(
		"id" uuid not null unique,
		"num" serial
	);

	insert into TMP_OBJ("id")
	select distinct ExportFrlloDataID
	from RDS.T_EXPORT_FRLLO_DATA fed 
	WHERE fed.ExportFrllo = "ExportId";

	loop
		if 
		not exists (select 1 from TMP_OBJ limit 1) 
			then exit;
		end if;
		update RDS.T_EXPORT_FRLLO_DATA as fed
		set FrlloId = null,
			FrlloStatus = null,
			FrlloError = null
		where exists(select 1 from TMP_OBJ t where t.id = fed.ExportFrlloDataID and t.num <= _Num);

		delete from TMP_OBJ where num <= _Num;
		_Num := _Num + 1000;
	end loop;
end;
$$;