IF EXISTS(SELECT *
            FROM SYSOBJECTS O, SYSUSERS U
           WHERE O.NAME = 'DTULTATU_AGENTETERCEFOR' 
             AND TYPE   = 'TR'
             AND U.UID  = O.UID
             AND U.NAME = USER)

   DROP TRIGGER DTULTATU_AGENTETERCEFOR
GO

CREATE TRIGGER DTULTATU_AGENTETERCEFOR
ON AGENTETERCEFOR
FOR INSERT, UPDATE, DELETE
AS
BEGIN
   IF (0 = (SELECT 
               COUNT(*) 
            FROM 
			   INSERTED))
   BEGIN
      INSERT INTO DELETECACHE 
         (NMTABLECACHE, 
          CDFILICACHE, 
          DSKEYCACHE, 
          DTULTATUCACH)
         (SELECT 
             'AGENTETERCEFOR',
             NULL,
             'CDFILIAL=' + CDFILIAL + ';' + 'CDFORNECED='  + CDFORNECED  + ';',
             GETDATE()
          FROM DELETED)
   END
   ELSE BEGIN
      UPDATE 
	     AGENTETERCEFOR
      SET 
	     DTULTATU = GETDATE()
      FROM 
	     INSERTED
      WHERE
	     (ISNULL(AGENTETERCEFOR.CDFILIAL, 0)     = ISNULL(INSERTED.CDFILIAL, 0))     AND
         (ISNULL(AGENTETERCEFOR.CDFORNECED, 0)   = ISNULL(INSERTED.CDFORNECED, 0))   AND
         (ISNULL(AGENTETERCEFOR.CDALMOXARIFE, 0) = ISNULL(INSERTED.CDALMOXARIFE, 0)) AND
         (ISNULL(AGENTETERCEFOR.CDLOCALESTOQ, 0) = ISNULL(INSERTED.CDLOCALESTOQ, 0));
   END;
END;
GO

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_AGENTETERCEFOR','5.85.xxx';
GO
