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

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

UPDATE RELACCODSSL 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_DTULTATURELACCODSSL' and
            type = 'D')
   ALTER TABLE RELACCODSSL DROP CONSTRAINT DEFAULT_DTULTATURELACCODSSL
GO

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

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_RELACCODSSL','6.05.000';
GO