do $$
begin
	if exists 
	(
		select * from pg_proc p
		where p.proname = 'sp_insert_t_fr_record'
	)
	then
		drop function public.sp_insert_t_fr_record(varchar(255), int, boolean, timestamp, timestamp, boolean, timestamp, timestamp, boolean);
	end if;
end;
$$ language plpgsql;

CREATE or Replace Function public.sp_Insert_T_FR_RECORD
(
	"Snils" varchar(255)
, 	"Version" int
, 	"Deleted" boolean
, 	"DateBegin" TimeStamp
, 	"DateEnd" TimeStamp
, 	"NSU" boolean
, 	"DateNsuBegin" TimeStamp
, 	"DateNsuEnd" TimeStamp
, 	"Nsus" boolean
,	"UpdateDate" TimeStamp
)
RETURNS void
As
$$
Begin
	if not exists 
	(
		select null from akuz.T_SNILS where Snils = "Snils"
	)
	then
		insert into akuz.T_SNILS(Snils) values ("Snils");
	end if;

	insert into akuz.T_FR_RECORD(FrRecordID, Snils, Version, Deleted, DateBegin, DateEnd, NSU, DateNsuBegin, DateNsuEnd, Nsus, UpdateDate)
	values (uuid_generate_v1(), "Snils", "Version", "Deleted", "DateBegin", "DateEnd", "NSU", "DateNsuBegin", "DateNsuEnd", "Nsus", "UpdateDate");
End;
$$ Language plpgsql;