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

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

UPDATE PRODSUBSIFILI 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_DTULTATUPRODSUBSIFILI' and
            type = 'D')
   ALTER TABLE PRODSUBSIFILI DROP CONSTRAINT DEFAULT_DTULTATUPRODSUBSIFILI
GO

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

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_PRODSUBSIFILI','6.27.000';
GO