DROP FUNCTION IF EXISTS fn_GetEntryToDoctorInspection;
CREATE FUNCTION fn_GetEntryToDoctorInspection
(
	"VisitID" uuid -- идентификатор AMBULANCE_DOCTOR_INSPECTION
)
RETURNS uuid AS
$BODY$
Begin
	RETURN 
	(
		Select eb.EntryBaseID
		From akuz.T_ENTRY_BASE eb
		Where eb.type_ = 595
		And Exists 
		(
			Select *
			From akuz.T_VISIT v
			Where v.VisitID = "VisitID"
			And eb.AmbulanceEvent = v.AmbulanceEvent
			And eb.EntryDate = v.VisitDate
			And eb.Resource = v.Post
		)
		Limit 1
	);
END $BODY$
  LANGUAGE plpgsql STABLE
  COST 100;
ALTER FUNCTION fn_getentrytodoctorinspection(uuid)
  OWNER TO postgres;