IF EXISTS(SELECT * FROM sysobjects O, sysusers U
          WHERE
            o.name = 'DTULTATUGRFO_GRUPFORN' and
            type = 'TR'                      and
            u.uid = o.uid           	     and
            u.name = USER)
   DROP TRIGGER DTULTATUGRFO_GRUPFORN
GO

CREATE TRIGGER DTULTATUGRFO_GRUPFORN
ON GRUPFORN
FOR INSERT, UPDATE, DELETE
AS
BEGIN
   IF (0 = (SELECT COUNT(*) FROM INSERTED)) BEGIN
      INSERT INTO DELETECACHE (NMTABLECACHE, CDFILICACHE, DSKEYCACHE, DTULTATUCACH)
			      (SELECT 'GRUPFORN', null, 'CDGRUPFORN=' + CDGRUPFORN + ';',  GETDATE() FROM DELETED)

   END
   ELSE BEGIN
      UPDATE GRUPFORN
      SET DTULTATUGRFO = GETDATE()
      FROM INSERTED
      WHERE (ISNULL(GRUPFORN.CDGRUPFORN,0  ) = ISNULL(INSERTED.CDGRUPFORN,0  )) AND
            (ISNULL(GRUPFORN.NMGRUPFORN,0  ) = ISNULL(INSERTED.NMGRUPFORN,0  )) ;
   END;
END;
GO


-- VERSAO --
EXEC ATUVEROBJ 'DTULTATUGRFO_GRUPFORN','5.40.000';
GO