/******************************************************************************/
/*                                                                            */
/* Trigger: DTULTATU_GRUPOBRIGOCOR                                            */
/* Tabela : GRUPOBRIGOCOR                                                     */
/*                                                                            */
/* Criação:                                                                   */
/*                                                                            */
/******************************************************************************/

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

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

UPDATE GRUPOBRIGOCOR 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 GRUPOBRIGOCOR DROP CONSTRAINT DEFAULT_DTULTATU
GO

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


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