IF EXISTS( SELECT 1 FROM SYS.OBJECTS WHERE NAME ='TRG_PDV_VD_DEV_NCC_LANC_CPL'AND TYPE = 'TR')
BEGIN
	DROP TRIGGER [dbo].[TRG_PDV_VD_DEV_NCC_LANC_CPL]
END
GO
CREATE TRIGGER [dbo].[TRG_PDV_VD_DEV_NCC_LANC_CPL] ON [dbo].[PDV_VD_DEV_NCC_LANC_CPL] AFTER INSERT, DELETE 
AS
BEGIN

	BEGIN				
		UPDATE NCC SET  STS_NCC=1
		FROM INSERTED i 
			INNER JOIN PDV_VD_DEV_NCC NCC ON
			I.CD_CTR_NCC=NCC.CD_CTR_NCC
			AND NCC.STS_NCC <> 5
	END	

	BEGIN
		UPDATE NCC SET  STS_NCC=0
		FROM DELETED d INNER JOIN PDV_VD_DEV_NCC NCC ON
			D.CD_CTR_NCC=NCC.CD_CTR_NCC
			AND NCC.STS_NCC <> 5
	END
END



