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

CREATE TRIGGER DTULTATU_RECETPCARD
ON
   RECETPCARD
FOR INSERT, UPDATE, DELETE

AS
BEGIN
  UPDATE
     PRATO
  SET
     DTULTATURECE = GETDATE()
  FROM
     INSERTED
  WHERE
     PRATO.CDPRATO = INSERTED.CDPRATO AND
     PRATO.CDFILAUXPRAT = INSERTED.CDFILAUXPRAT;
  IF @@ROWCOUNT = 0
  BEGIN
     UPDATE
        PRATO
     SET
        DTULTATURECE = GETDATE()
     FROM
        DELETED
     WHERE
        PRATO.CDPRATO = DELETED.CDPRATO AND
        PRATO.CDFILAUXPRAT = DELETED.CDFILAUXPRAT;
  END
  IF (0 <> (SELECT COUNT(*) FROM DELETED)) BEGIN /* UPDATE */
     UPDATE
        RECETPCARD
     SET DTULTATURECPTP = GETDATE()
     FROM
        INSERTED, DELETED
     WHERE
        RECETPCARD.CDFILAUXPRAT = INSERTED.CDFILAUXPRAT AND
        RECETPCARD.CDPRATO      = INSERTED.CDPRATO      AND
        RECETPCARD.CDPRODUTO    = INSERTED.CDPRODUTO    AND
        RECETPCARD.CDFILCENTPME = INSERTED.CDFILCENTPME AND
        RECETPCARD.CDTIPOCARD   = INSERTED.CDTIPOCARD   AND
        RECETPCARD.CDFILAUXPRAT = DELETED.CDFILAUXPRAT  AND
        RECETPCARD.CDPRATO      = DELETED.CDPRATO       AND
        RECETPCARD.CDPRODUTO    = DELETED.CDPRODUTO     AND
        RECETPCARD.CDFILCENTPME = DELETED.CDFILCENTPME  AND
        RECETPCARD.CDTIPOCARD   = DELETED.CDTIPOCARD;
  END ELSE BEGIN  /* INSERT */
     UPDATE
        RECETPCARD
     SET DTULTATURECPTP = GETDATE()
     FROM
        INSERTED
     WHERE
        RECETPCARD.CDFILAUXPRAT = INSERTED.CDFILAUXPRAT AND
        RECETPCARD.CDPRATO      = INSERTED.CDPRATO      AND
        RECETPCARD.CDPRODUTO    = INSERTED.CDPRODUTO    AND
        RECETPCARD.CDFILCENTPME = INSERTED.CDFILCENTPME AND
        RECETPCARD.CDTIPOCARD   = INSERTED.CDTIPOCARD;
  END;
END;
GO