IF EXISTS (SELECT * FROM SYS.TRIGGERS WHERE NAME = 'TRG_EST_PROD_CPL_DEMANDA_PROC_CPL_BLOQUEIO_EXCLUI_PENDENCIA')
begin
	DROP TRIGGER [dbo].[TRG_EST_PROD_CPL_DEMANDA_PROC_CPL_BLOQUEIO_EXCLUI_PENDENCIA]
end
go
CREATE TRIGGER [dbo].[TRG_EST_PROD_CPL_DEMANDA_PROC_CPL_BLOQUEIO_EXCLUI_PENDENCIA] ON [dbo].[EST_PROD_CPL_DEMANDA_PROC_CPL_BLOQUEIO] FOR INSERT AS
DELETE FROM p FROM EST_PROD_CPL_DEMANDA_PROC_CPL_LIB_PENDENTE p WHERE EXISTS
    (SELECT * FROM INSERTED WHERE CD_CTR_PROC_CPL=p.CD_CTR_PROC_CPL)

DELETE FROM p FROM EST_PROD_CPL_DEMANDA_PROC_CPL_PENDENTE_APLICAR_MEDIAF p WHERE EXISTS
    (SELECT * FROM INSERTED WHERE CD_CTR_PROC_CPL=p.CD_CTR_PROC_CPL)
