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

--CREATE or Replace Function public.sp_Insert_T_FR_PATIENT
--(
--	"Snils" varchar(255)
--,	"FirstName" varchar(255)
--, 	"LastName" varchar(255)
--, 	"FatherName" varchar(255)
--, 	"Birthday" TimeStamp
--, 	"Sex" int
--)
--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;

--	if exists 
--	(
--		select null from akuz.T_FR_PATIENT where Snils = "Snils"
--	)
--	then
--		update	akuz.T_FR_PATIENT 
--		set	FirstName = "FirstName"
--		,	LastName = "LastName"
--		,	FatherName = "FatherName"
--		, 	Birthday = "Birthday"
--		, 	Sex = "Sex"
--		where Snils = "Snils";
--	else
--		insert into akuz.T_FR_PATIENT(FrPatientID, Snils, FirstName, LastName, FatherName, Birthday, Sex)
--		values(uuid_generate_v1(), "Snils", "FirstName", "LastName", "FatherName", "Birthday", "Sex");
--	end if;
--End;
--$$ Language plpgsql;