---------------- triggers para auditoria de alteração do status de credito do conveniado ----------------------------
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRG_PDV_MAPA_RESUMO_UPD_CONSLD_VD]'))

/****** Object:  Trigger [TRG_PDV_MAPA_RESUMO_UPD_CONSLD_VD]    Script Date: 29/05/2015 09:40:35 ******/
DROP TRIGGER [dbo].[TRG_PDV_MAPA_RESUMO_UPD_CONSLD_VD]
GO
CREATE TRIGGER [dbo].[TRG_PDV_MAPA_RESUMO_UPD_CONSLD_VD] ON [dbo].[PDV_MAPA_RESUMO] AFTER INSERT,UPDATE
AS
begin
DELETE
	PDV_CONSLD_ERROS
FROM
	PDV_CONSLD_ERROS INNER JOIN INSERTED r ON
	PDV_CONSLD_ERROS.CD_EMP = r.CD_EMP AND
	PDV_CONSLD_ERROS.CD_FILIAL = r.CD_FILIAL AND
	PDV_CONSLD_ERROS.DT_MOV = r.DT_ECF AND
	PDV_CONSLD_ERROS.CD_CX = r.CD_CX
WHERE
	PDV_CONSLD_ERROS.TP_CUPOM = 2
end
GO
GO
DELETE
	PDV_CONSLD_ERROS
from
	PDV_CONSLD_ERROS INNER JOIN PDV_MAPA_RESUMO ON
	PDV_CONSLD_ERROS.CD_EMP = PDV_MAPA_RESUMO.CD_EMP AND
	PDV_CONSLD_ERROS.CD_FILIAL = PDV_MAPA_RESUMO.CD_FILIAL AND
	PDV_CONSLD_ERROS.DT_MOV = PDV_MAPA_RESUMO.DT_ECF AND
	PDV_CONSLD_ERROS.CD_CX = PDV_MAPA_RESUMO.CD_CX
where
	PDV_CONSLD_ERROS.TP_CUPOM = 2
go
go
DELETE
	PDV_CONSLD_ERROS
where
	PDV_CONSLD_ERROS.DT_MOV <'2015-05-01'
