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

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

UPDATE MOVPONTOCONS 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_DTULTATUMOVPONTOCONS' and
            type = 'D')
   ALTER TABLE MOVPONTOCONS DROP CONSTRAINT DEFAULT_DTULTATUMOVPONTOCONS
GO

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

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_MOVPONTOCONS','5.75.000';
GO