IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_GetDrugFormLim]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_GetDrugFormLim]
GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- =============================================
---- Author:		Marat Zigangaraev
---- Create date: 16.10.2010
---- Description:	Возвращает цену формы ЛС
---- =============================================
--CREATE FUNCTION [dbo].[fn_GetDrugFormLim] 
--(
--	@Code int -- код лекарственной формы (id T_DRUG_FORM)
--	,@OnDate datetime = NULL
--	,@KindOfLim int = NULL
--)
--RETURNS int
--AS
--BEGIN
--	RETURN 
--	(
--		Select Top 1 l.Code
--		From [AKUZ].T_DRUG_FORM_LIM l WITH(NOLOCK)
--		Where l.DrugForm = @Code
--		And l.DateBegin <= IsNull(@OnDate, getdate()) 
--		And ((IsNull(@KindOfLim,0)=0 AND l.Fed=1) Or (IsNull(@KindOfLim,0)=1 AND l.Psn=1) Or (IsNull(@KindOfLim,0)=2 AND l.Reg=1))
--		Order by l.DateBegin desc
--	)
--END
--GO
