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

CREATE TRIGGER DTULTATU_BASECALCTPOPER
ON BASECALCTPOPER
FOR INSERT, UPDATE, DELETE
AS
BEGIN
   IF (0 = (SELECT COUNT(*) FROM INSERTED)) BEGIN
      INSERT INTO DELETECACHE (NMTABLECACHE, CDFILICACHE, DSKEYCACHE, DTULTATUCACH)
			      (SELECT 'BASECALCTPOPER', '', 'CDIMPOSTO='+CDIMPOSTO+';NRSEQBCALTPOP='+NRSEQBCALTPOP+';IDENTRSAIDOP='+
				       IDENTRSAIDOP+';CDTIPOOPER='+CDTIPOOPER+';', CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),103)+' '+CONVERT(VARCHAR,GETDATE(),108)) FROM DELETED)
   END
   ELSE BEGIN
      UPDATE BASECALCTPOPER
         SET DTULTATU = CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),103)+' '+CONVERT(VARCHAR,GETDATE(),108))
        FROM INSERTED
       WHERE (ISNULL(BASECALCTPOPER.CDIMPOSTO,0) = ISNULL(INSERTED.CDIMPOSTO,0))
         AND (ISNULL(BASECALCTPOPER.NRSEQBCALTPOP,0) = ISNULL(INSERTED.NRSEQBCALTPOP,0))
	 AND (ISNULL(BASECALCTPOPER.IDENTRSAIDOP,0) = ISNULL(INSERTED.IDENTRSAIDOP,0))
	 AND (ISNULL(BASECALCTPOPER.CDTIPOOPER,0)   = ISNULL(INSERTED.CDTIPOOPER,0));
   END;
END;
GO

UPDATE BASECALCTPOPER 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_DTULTATUBASECALCTPOPER' and
            type = 'D')
   ALTER TABLE BASECALCTPOPER DROP CONSTRAINT DEFAULT_DTULTATUBASECALCTPOPER
GO

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

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_BASECALCTPOPER','6.19.000';
GO