/******************************************************************************/
/******************************************************************************/
/*                                                                            */
/* Trigger: DTULTATU_COLLAYOUTIMP                                             */
/* Tabela : COLLAYOUTIMP                                                      */
/*                                                                            */
/* Criação:                                                                   */
/* 09/05/2011 - CAIO ECKHARDT PASCHOAL                                        */
/*                                                                            */
/******************************************************************************/
IF EXISTS (SELECT * FROM SYSOBJECTS O, SYSUSERS U
           WHERE O.NAME = 'DTULTATU_COLLAYOUTIMP' AND
                 TYPE   = 'TR' 	               AND
	              U.UID  = O.UID           AND
	              U.NAME = USER)
DROP TRIGGER DTULTATU_COLLAYOUTIMP
GO

CREATE TRIGGER DTULTATU_COLLAYOUTIMP
ON COLLAYOUTIMP
FOR INSERT, UPDATE, DELETE
AS
BEGIN
   IF (0 = (SELECT COUNT(*) FROM INSERTED)) BEGIN
      INSERT INTO DELETECACHE (NMTABLECACHE, CDFILICACHE, DSKEYCACHE, DTULTATUCACH)
			      (SELECT 'COLLAYOUTIMP', '', 'CDLAYOUTIMP='+CDLAYOUTIMP+';NRSEQTABELAY='+NRSEQTABELAY+';IDFUNARQLAY='+ IDFUNARQLAY + ';NRSEQCOLULAY=' + NRSEQCOLULAY+';', GETDATE() FROM DELETED)
   END IDFUNARQLAY + NRSEQCOLULAY
   ELSE BEGIN
      UPDATE COLLAYOUTIMP
      SET DTULTATUCOLL = GETDATE()
      FROM INSERTED
      WHERE (ISNULL(COLLAYOUTIMP.CDLAYOUTIMP,0) = ISNULL(INSERTED.CDLAYOUTIMP,0)) AND
         (ISNULL(COLLAYOUTIMP.NRSEQTABELAY,0) = ISNULL(INSERTED.NRSEQTABELAY,0)) AND 
         (ISNULL(COLLAYOUTIMP.NMCCUSCLIE,0) = ISNULL(INSERTED.NMCCUSCLIE,0));
   END;
END;
GO


ALTER TABLE COLLAYOUTIMP MODIFY DTULTATUCOLL DEFAULT SYSDATE
GO

UPDATE COLLAYOUTIMP SET DTULTATUCOLL = SYSDATE WHERE DTULTATUCOLL IS NULL
GO

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_COLLAYOUTIMP','5.20.000';
GO 