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

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

UPDATE PRODEXTERNO 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_DTULTATUPRODEXTERNO' and
            type = 'D')
   ALTER TABLE PRODEXTERNO DROP CONSTRAINT DEFAULT_DTULTATUPRODEXTERNO
GO

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

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_PRODEXTERNO','6.19.000';
GO