/****** Object:  Trigger [TRG_EST_PROD_TRANSF_LACRE_NAO_CHECKIN_ENTRADA_EST_NF_ENT_IT_DEVOLUCAO]    Script Date: 07/05/2014 11:44:15 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRG_EST_PROD_TRANSF_LACRE_NAO_CHECKIN_ENTRADA_EST_NF_ENT_IT_DEVOLUCAO]'))
DROP TRIGGER [dbo].[TRG_EST_PROD_TRANSF_LACRE_NAO_CHECKIN_ENTRADA_EST_NF_ENT_IT_DEVOLUCAO]
GO
GO
GO

CREATE TRIGGER [dbo].[TRG_EST_PROD_TRANSF_LACRE_NAO_CHECKIN_ENTRADA_EST_NF_ENT_IT_DEVOLUCAO] ON [dbo].[EST_PROD_TRANSF_LACRE_NAO_CHECKIN_ENTRADA_EST_NF_ENT_IT_DEVOLUCAO] 
FOR INSERT
AS
BEGIN TRY
	--Deletando caso existia o registro
	DELETE 
		p
	FROM 
		EST_PROD_TRANSF_LACRE_NAO_CHECKIN_ENTRADA_TRATAMENTO_PENDENTE p INNER JOIN INSERTED i ON
		p.CD_EMP = i.CD_EMP AND
		p.CD_FILIAL = i.CD_FILIAL AND
		p.CD_CTR = i.CD_CTR AND
		p.CD_LACRE = i.CD_LACRE
	--Fim
END TRY
BEGIN CATCH
	INSERT INTO ADM_ERRO (numero, severidade, estado, rotina, linha, mensagem, dtErro)
		SELECT
			ERROR_NUMBER() AS numero,
			ERROR_SEVERITY() AS severidade,
			ERROR_STATE() AS estado,
			ERROR_PROCEDURE() AS rotina,
			ERROR_LINE() AS linha,
			ERROR_MESSAGE() AS mensagem,
			GETDATE() AS dtErro;
END CATCH

