IF EXISTS
(
	SELECT *
	FROM   sys.objects
	WHERE  object_id = OBJECT_ID(N'[dbo].[fn_GetCommentDrugRequestList]')
		   AND [type] IN(N'FN', N'IF', N'TF', N'FS', N'FT')
)
	BEGIN
		DROP FUNCTION [dbo].[fn_GetCommentDrugRequestList]
	END
--GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- =============================================
---- Author:		Anatoliy.Pokus
---- Create date: 08.09.2017
---- Description:	Возвращает заявочный лист
---- =============================================


--CREATE FUNCTION [dbo].[fn_GetCommentDrugRequestList]
--(@ID uniqueidentifier
--)
--RETURNS uniqueidentifier
--AS
--	BEGIN
--		RETURN
--		(
--			SELECT
--			(
--				SELECT TOP 1 [drl].[BaseDrugRequestItemID]
--				FROM         [AKUZ].[T_BASE_DRUG_REQUEST_ITEM] AS [drl]
--							 LEFT JOIN [AKUZ].[T_REQUEST_CATALOG] AS [rc] ON [rc].[BaseDrugRequestItemID] = [drl].[BaseDrugRequestItemID]
--				WHERE        [drl].[RequestCode] = [dd].[RequestCode]
--							 AND [drl].[AppListBeginDate]/*Элемент зс*/ <= bdr.[BeginPeriod]/*Заявка*/
--							 AND [drl].[AppListEndDate]/*Элемент зс*/ >= bdr.[EndPeriod]/*Заявка*/
--							 AND ([rc].[BaseDrugRequestItemID] IS NULL
--								  OR [dr].[RequestType] = [rc].[BenefitType])
--			)
--			FROM [AKUZ].T_DRUG_REQUEST_COMMENTS AS dd
--				 JOIN [AKUZ].T_DRUG_REQUEST AS dr ON dd.DrugRequest = dr.DrugRequestID
--				 join [AKUZ].T_BASE_DRUG_REQUEST AS bdr ON dd.DrugRequest = bdr.DrugRequestID
--			WHERE [dd].[DrugRequestCommentsID] = @ID
--		)
--	END
--GO