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

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

UPDATE TIPOCONSLOJA 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_DTULTATUTIPOCONSLOJA' and
            type = 'D')
   ALTER TABLE TIPOCONSLOJA DROP CONSTRAINT DEFAULT_DTULTATUTIPOCONSLOJA
GO

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

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_TIPOCONSLOJA','6.02.000';
GO