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

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

UPDATE SETORPRODINTER 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_DTULTATUSETORPRODINTER' and
            type = 'D')
   ALTER TABLE SETORPRODINTER DROP CONSTRAINT DEFAULT_DTULTATUSETORPRODINTER
GO

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

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_SETORPRODINTER','5.85.000';
GO