IF EXISTS (SELECT * FROM SYS.VIEWS WHERE NAME = 'V_EST_PROD_TBL_DESC' )
	BEGIN
		DROP VIEW V_EST_PROD_TBL_DESC
	END
GO
--SELECT MAX(PERC_DESC), CD_FILIAL FROM V_EST_PROD_TBL_DESC WHERE CD_FILIAL = 12 AND CD_PROD = 36571 GROUP BY CD_FILIAL
CREATE VIEW [dbo].[V_EST_PROD_TBL_DESC] AS 
SELECT
	CD_PROD,
	PERC_DESC,
	CD_ARV_MERC_LINHA,
	CD_FILIAL
FROM
	V_EST_PROD_TBL_DESC_CADASTRO
UNION 
SELECT
	CD_PROD,
	PERC_DESC,
	CD_ARV_MERC_LINHA,
	CD_FILIAL
FROM
	V_EST_PROD_TBL_DESC_PROGRAMADO
WHERE
	DT_FIM  >= CONVERT(DATE,GETDATE())
	AND DT_INI <= CONVERT(DATE,GETDATE())
UNION --DESCONTO ANTIGO
SELECT 
	B.CD_PROD,
	B.PERC_DESC,
	ARV.CD_ARV_MERC_LINHA,
	C.CD_FILIAL
FROM  
	EST_PROD_PRECO_DESC_PRG A 
	INNER JOIN EST_PROD_PRECO_DESC_PRG_CPL B ON 
	A.CD_PRECO_PRG  =B.CD_PRECO_PRG
	INNER JOIN EST_PROD_PRECO_DESC_PRG_PRC_FILIAL C ON 
	A.CD_PRECO_PRG  =C.CD_PRECO_PRG
	INNER JOIN EST_PROD_EST_ARV_MERCADOLOGICA ARV ON
	B.CD_EMP = ARV.CD_EMP
	AND B.CD_PROD = ARV.CD_PROD  
WHERE
    A.DT_VENCTO  >= CONVERT(DATE,GETDATE())
	and A.DT_INI <= CONVERT(DATE,GETDATE());
	  
go