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

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

UPDATE VOUCHERVND 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_DTULTATUVOUCHERVND' and
            type = 'D')
   ALTER TABLE VOUCHERVND DROP CONSTRAINT DEFAULT_DTULTATUVOUCHERVND
GO

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

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_VOUCHERVND','5.75.000';
GO