/*#########################################################################################*/

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

CREATE TRIGGER DTULTATU_PRATFILI
ON
   PRATFILI
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
END
;
GO

