/******************************************************************************/
/*                                                                            */
/* Trigger: DTULTATU_PRODOBRIGOCOR                                            */
/* Tabela : PRODOBRIGOCOR                                                     */
/*                                                                            */
/* Criação:                                                                   */
/*                                                                            */
/******************************************************************************/

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

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

UPDATE PRODOBRIGOCOR 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_DTULTATU' and
            type = 'D')
   ALTER TABLE PRODOBRIGOCOR DROP CONSTRAINT DEFAULT_DTULTATU
GO

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


-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_PRODOBRIGOCOR','6.19.001';
GO
