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

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

UPDATE BASECALC 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_DTULTATUBASECALC' and
            type = 'D')
   ALTER TABLE BASECALC DROP CONSTRAINT DEFAULT_DTULTATUBASECALC
GO

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

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