if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fn_GetLastVisit]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fn_GetLastVisit]
GO

--create function [dbo].fn_GetLastVisit (@VisitID uniqueidentifier)
--returns uniqueidentifier
--as
--begin
--	declare @ret uniqueidentifier

--	select top 1
--		@ret = t1.VisitID
--	from
--		[AKUZ].T_DOCTOR_INSPECTION_RE t1 with(nolock)
--			inner join
--		[AKUZ].T_VISIT t2 with(nolock) on t1.VisitID = t2.VisitID and t1.DoctorInspection = @VisitID
--	order by VisitDate desc

--	if @ret is null return @VisitID
--	return @ret
--end
--GO