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

   DROP TRIGGER DTULTATU_PRODRETI
GO

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

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_PRODRETI','6.35.020';
GO
