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

CREATE TRIGGER DTULTATU_COMBINAPROD
ON COMBINAPROD
FOR INSERT, UPDATE, DELETE
AS
BEGIN
   IF (0 = (SELECT COUNT(*) FROM INSERTED)) BEGIN
      INSERT INTO DELETECACHE (NMTABLECACHE, CDFILICACHE, DSKEYCACHE, DTULTATUCACH)
			      (SELECT 'COMBINAPROD', '', 'CDCAMPPROMO='+CDCAMPPROMO+';CDPRODPRIN='+CDPRODPRIN+';CDPRODCOMB='+CDPRODCOMB+';', CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),103)+' '+CONVERT(VARCHAR,GETDATE(),108)) FROM DELETED)
   END
   ELSE BEGIN
      UPDATE COMBINAPROD
         SET DTULTATU = CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),103)+' '+CONVERT(VARCHAR,GETDATE(),108))
        FROM INSERTED
       WHERE (ISNULL(COMBINAPROD.CDCAMPPROMO,0) = ISNULL(INSERTED.CDCAMPPROMO,0))
         AND (ISNULL(COMBINAPROD.CDPRODPRIN ,0) = ISNULL(INSERTED.CDPRODPRIN ,0))
		 AND (ISNULL(COMBINAPROD.CDPRODCOMB ,0) = ISNULL(INSERTED.CDPRODCOMB ,0));
   END;
END;
GO

UPDATE COMBINAPROD SET DTULTATU = CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),103)+' '+CONVERT(VARCHAR,GETDATE(),108)) WHERE DTULTATU IS NULL
GO

IF EXISTS(SELECT * FROM sysobjects O
          WHERE
            o.name = 'DEFAULT_DTULTATUCOMBINAPROD' and
            type = 'D')
   ALTER TABLE COMBINAPROD DROP CONSTRAINT DEFAULT_DTULTATUCOMBINAPROD
GO

ALTER TABLE COMBINAPROD ADD CONSTRAINT DEFAULT_DTULTATUCOMBINAPROD
DEFAULT CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),103)+' '+CONVERT(VARCHAR,GETDATE(),108))
FOR DTULTATU
GO

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_COMBINAPROD','6.10.000';
GO