
/****** Object:  StoredProcedure [dbo].[sp_REAP1_MatchPromotions_GetMasterNames]    Script Date: 2020/05/06 09:43:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



ALTER PROCEDURE [dbo].[sp_REAP1_MatchPromotions_GetMasterNames] 
	@unitEANs VARCHAR(MAX) = '',
	@purchaseDate VARCHAR(MAX),
	@areas VARCHAR(MAX)
AS

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @query VARCHAR(MAX);

	SELECT @query = 'SELECT DISTINCT PromotionItems_PromotionItem_UnitEAN, PromotionItems_PromotionItem_ProductMasterName 
	FROM [publ_PromotionItems]
	WHERE PromotionItems_PromotionItem_UnitEAN IN (' 
	+ @unitEANs
	+ ') AND CONVERT(DATE, ' 
	+ CHAR(39) + @purchaseDate + CHAR(39) 
	+', 111) BETWEEN CONVERT(DATE, PromotionItems_PromotionDetails_PromotionStartDate, 111) 
	AND CONVERT(DATE, PromotionItems_PromotionDetails_PromotionEndDate, 111)
	AND PromotionItems_PromotionItems_Area IN (' + @areas + ')';

	EXECUTE (@query);

END
GO


