IF NOT OBJECT_ID('TRG_EST_NF_ENT_CANCEL_PEND_NFE_LOTE_ENT_ERR') IS NULL
	BEGIN 
		DROP TRIGGER TRG_EST_NF_ENT_CANCEL_PEND_NFE_LOTE_ENT_ERR
	END

GO

------------------------------------INSERO EM EST_NF_ENT_NFE_PEND------------------------------------      
CREATE TRIGGER [dbo].[TRG_EST_NF_ENT_CANCEL_PEND_NFE_LOTE_ENT_ERR] ON [dbo].[EST_NFE_LOTE_ENT_ERR] FOR INSERT AS      

begin      
	--Solicitando o cancelamento da nota fiscal case seja uma nota denegada.		
	INSERT INTO EST_NF_ENT_CANCEL_SOLIC (CD_EMP, CD_FILIAL, CD_NF, CD_USU, CD_MT_CANCEL, ST_DENEGADA, DT_CANCEL, DT_CAD)
		SELECT 
			T.CD_EMP, T.CD_FILIAL, T.CD_NF, 1, 0, 1, CONVERT(VARCHAR(10), GETDATE(),101), CONVERT(VARCHAR(10), GETDATE(),101)
		FROM INSERTED I
			INNER JOIN EST_NFE_LOTE_ENT T ON 
				I.CD_NFE_LOTE		  = T.CD_NFE_LOTE
				AND I.CD_NFE_LOTE_ENT = T.CD_NFE_LOTE_ENT
		WHERE I.CD_MOT_ERR IN (205,301,302)		
	 
end

GO
 