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

   DROP TRIGGER DTULTATU_AGENTETERCECLI
GO

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

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