
IF EXISTS (SELECT * FROM SYS.TRIGGERS WHERE NAME = 'TRG_INTEGRA_ULTRAFARMA_CONTROLE_STATUS')
BEGIN
	DROP TRIGGER TRG_INTEGRA_ULTRAFARMA_CONTROLE_STATUS
END

GO

CREATE TRIGGER TRG_INTEGRA_ULTRAFARMA_CONTROLE_STATUS ON INTEGRA_ULTRAFARMA_CONTROLE 
	AFTER INSERT, UPDATE
AS
BEGIN
	IF NOT UPDATE (STATUS)
		RETURN;

	UPDATE U
	SET
		STATUS=0
	FROM
		INTEGRA_ULTRAFARMA_CONTROLE U
		join INSERTED I ON
		U.CD_CTR =I.CD_CTR
	WHERE 
		I.STATUS=2;

END