/******************************************************************************/
/*                                                                            */
/* Trigger: DTULTATU_CARDCICLO                                                */
/* Tabela : CARDCICLO                                                         */
/*                                                                            */
/******************************************************************************/

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

CREATE TRIGGER DTULTATU_CARDCICLO
ON CARDCICLO
FOR INSERT, UPDATE, DELETE
AS
BEGIN
   IF (0 = (SELECT COUNT(*) FROM INSERTED)) BEGIN
      INSERT INTO DELETECACHE (NMTABLECACHE, CDFILICACHE, DSKEYCACHE, DTULTATUCACH)
			      (SELECT 'CARDCICLO', null, 'CDCARDCICLO =' + CDCARDCICLO + ';', GETDATE() FROM DELETED)
   END
   ELSE BEGIN
      UPDATE CARDCICLO
         SET DTULTATUCRDCICL = GETDATE()
        FROM INSERTED
       WHERE (ISNULL(CARDCICLO.CDCARDCICLO,0)   = ISNULL(INSERTED.CDCARDCICLO,0))
         AND (ISNULL(CARDCICLO.NMCARDCICLO,0)   = ISNULL(INSERTED.NMCARDCICLO,0))
         AND (ISNULL(CARDCICLO.NRSEMCICLO,0)    = ISNULL(INSERTED.NRSEMCICLO,0))
         AND (ISNULL(CARDCICLO.NRDIAINICICLO,0) = ISNULL(INSERTED.NRDIAINICICLO,0))
         AND (ISNULL(CARDCICLO.IDATIVCICLO,0)   = ISNULL(INSERTED.IDATIVCICLO,0));
   END;
END;
GO
