IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRG_EST_NFE_PEREC_PERDA_ROUBO_TRANSP_FORN_PEND]'))
	DROP TRIGGER [dbo].[TRG_EST_NFE_PEREC_PERDA_ROUBO_TRANSP_FORN_PEND]
GO

CREATE TRIGGER [dbo].[TRG_EST_NFE_PEREC_PERDA_ROUBO_TRANSP_FORN_PEND] ON [dbo].[EST_NFE_PEREC_PERDA_ROUBO_TRANSP_FORN] FOR INSERT AS

--Pendência de emissao de carta de correcao
INSERT INTO EST_NFE_PEREC_PERDA_ROUBO_TRANSP_FORN_PEND (CD_CTR_PERDA_ROUBO_TRANSP_FORN, DT_CAD, PEND_SOLUC)
	SELECT 
		t.CD_CTR_PERDA_ROUBO_TRANSP_FORN, 
		GETDATE(),
		0
    FROM 
    	INSERTED t 
	WHERE 				
		NOT EXISTS (SELECT 
						pend.CD_CTR_PERDA_ROUBO_TRANSP_FORN 
					FROM 
						EST_NFE_PEREC_PERDA_ROUBO_TRANSP_FORN_PEND pend
					WHERE 
						t.CD_CTR_PERDA_ROUBO_TRANSP_FORN = pend.CD_CTR_PERDA_ROUBO_TRANSP_FORN);



