--do $$
--begin
--	if exists 
--	(
--		select * from pg_proc p
--		where p.proname = 'sp_insert_t_fr_document'
--	)
--	then
--		drop function public.sp_insert_t_fr_document(varchar(255), varchar(255), varchar(255), varchar(255));
--	end if;
--end;
--$$ language plpgsql;
---- Хранимки для обновления регистра льготников
--CREATE or Replace Function public.sp_Insert_T_FR_DOCUMENT
--(
--	"Snils" varchar(255)
--, 	"Name" varchar(255)
--, 	"Number" varchar(255)
--, 	"Benefits" varchar(255)
--)
--RETURNS void
--As 
--$$
--Declare	bid UUID;
--Begin
--	select t.BenefitsID 
--	from akuz.T_BENEFITS t
--	into bid
--	where t.BenefitCode = "Benefits"
--	and t.BenefitViews = 19001;

--	if not exists(select null from akuz.T_SNILS s where s.Snils = "Snils")
--	then
--		insert into akuz.T_SNILS(Snils) values ("Snils");
--	end if;

--	if exists
--	(
--		select null from akuz.T_FR_DOCUMENT fd where fd.Snils = "Snils" and fd.Benefits = bid
--	)
--	then
--		update	akuz.T_FR_DOCUMENT fd
--		set	Name = "Name"
--		,	Number = "Number"
--		where fd.Snils = "Snils"
--		and fd.Benefits = bid;
--	else
--		insert into akuz.T_FR_DOCUMENT(FrDocumentID, Name, Number, Benefits, Snils)
--		values (uuid_generate_v1(), "Name", "Number", bid, "Snils");
--	end if;
--End;
--$$ Language plpgsql;