IF EXISTS(SELECT * FROM sysobjects O, sysusers U
          WHERE
            o.name = 'DTULTATU_MOVBENEFICIOCTR'  and
            type = 'TR'               and
            u.uid = o.uid             and
            u.name = USER)
   DROP TRIGGER DTULTATU_MOVBENEFICIOCTR
GO

CREATE TRIGGER DTULTATU_MOVBENEFICIOCTR
ON MOVBENEFICIOCTR
FOR INSERT, UPDATE, DELETE
AS
BEGIN
   IF (0 = (SELECT COUNT(*) FROM INSERTED)) BEGIN
      INSERT INTO DELETECACHE (NMTABLECACHE, CDFILICACHE, DSKEYCACHE, DTULTATUCACH)
			      (SELECT 'MOVBENEFICIOCTR', '', 'CDCLIENTE='+CDCLIENTE+';CDCONSUMIDOR='+CDCONSUMIDOR+';CDCAMPANHA='+CDCAMPANHA+';NRSEQPRODCAMP='+NRSEQPRODCAMP+';NRSEQBENEFICIO='+NRSEQBENEFICIO+';', CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),103)+' '+CONVERT(VARCHAR,GETDATE(),108)) FROM DELETED)
   END
   ELSE BEGIN
      UPDATE MOVBENEFICIOCTR
         SET DTULTATU = CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),103)+' '+CONVERT(VARCHAR,GETDATE(),108))
        FROM INSERTED
       WHERE (ISNULL(MOVBENEFICIOCTR.CDCLIENTE     ,0) = ISNULL(INSERTED.CDCLIENTE     ,0))
         AND (ISNULL(MOVBENEFICIOCTR.CDCONSUMIDOR  ,0) = ISNULL(INSERTED.CDCONSUMIDOR  ,0))
         AND (ISNULL(MOVBENEFICIOCTR.CDCAMPANHA    ,0) = ISNULL(INSERTED.CDCAMPANHA    ,0))
		 AND (ISNULL(MOVBENEFICIOCTR.NRSEQPRODCAMP ,0) = ISNULL(INSERTED.NRSEQPRODCAMP ,0))
		 AND (ISNULL(MOVBENEFICIOCTR.NRSEQBENEFICIO,0) = ISNULL(INSERTED.NRSEQBENEFICIO,0));
   END;
END;
GO

UPDATE MOVBENEFICIOCTR SET DTULTATU = CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),103)+' '+CONVERT(VARCHAR,GETDATE(),108)) WHERE DTULTATU IS NULL
GO

IF EXISTS(SELECT * FROM sysobjects O
          WHERE
            o.name = 'DEFAULT_DTULTATUMOVBENEFICIOCTR' and
            type = 'D')
   ALTER TABLE MOVBENEFICIOCTR DROP CONSTRAINT DEFAULT_DTULTATUMOVBENEFICIOCTR
GO

ALTER TABLE MOVBENEFICIOCTR ADD CONSTRAINT DEFAULT_DTULTATUMOVBENEFICIOCTR
DEFAULT CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),103)+' '+CONVERT(VARCHAR,GETDATE(),108))
FOR DTULTATU
GO

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_MOVBENEFICIOCTR','6.05.000';
GO