IF EXISTS( SELECT * FROM SYS.OBJECTS 
			WHERE NAME ='TRG_EST_NF_ENT_DANFE_PROVISORIO_EST_PED_CP_INCLUI_PENDENCIA'AND TYPE = 'TR')
BEGIN
	DROP TRIGGER dbo.TRG_EST_NF_ENT_DANFE_PROVISORIO_EST_PED_CP_INCLUI_PENDENCIA 
END
 
SET ANSI_NULLS ON
GO 
SET QUOTED_IDENTIFIER ON
GO 

CREATE TRIGGER [dbo].[TRG_EST_NF_ENT_DANFE_PROVISORIO_EST_PED_CP_INCLUI_PENDENCIA] ON [dbo].[EST_NF_ENT_DANFE_PROVISORIO_EST_PED_CP] FOR  DELETE AS
BEGIN
	  
	INSERT INTO EST_NF_ENT_DANFE_PROVISORIO_SEM_PEDIDO (CD_CTR, DT_CAD)
	SELECT
		D.CD_CTR,
		getdate() DT_CAD
	FROM 
		DELETED D
		INNER JOIN EST_NF_ENT_DANFE_PROVISORIO W ON 
		D.CD_CTR = W.CD_CTR
		LEFT JOIN EST_NF_ENT_DANFE_PROVISORIO_SEM_PEDIDO P ON 
		D.CD_CTR = P.CD_CTR
	WHERE
		P.CD_CTR IS NULL

END  
GO


