DROP FUNCTION IF EXISTS fn_GetEmergencyEvent;
CREATE FUNCTION fn_GetEmergencyEvent("HospReestrItemID" UUID)
RETURNS UUID AS
 $$
 Begin
	return 
	(
		SELECT mv.EmergencyEvent
		FROM akuz.T_HOSPITAL_REESTR_ITEM t
		Inner JOIN
			akuz.T_HOSPITAL_RECORD hr 
			ON t.MovementResult = hr.MedicalRecordID
		Inner JOIN
			akuz.T_MOVEMENT mv 
			On hr.MedicalcardRegistration = mv.MovementID
		WHERE t.HospitalReestrItemID = "HospReestrItemID"
		limit 1
	);
END $$
language plpgsql stable;