/******************************************************************************/
/******************************************************************************/
/*                                                                            */
/* Trigger: DTULTATU_TIPOSANGRIA                                              */
/* Tabela : TIPOSANGRIA                                                       */
/*                                                                            */
/* Criação:                                                                   */
/* 09/05/2011 - LUCIANA CORREA                                                */
/*                                                                            */
/******************************************************************************/
IF EXISTS (SELECT * FROM SYSOBJECTS O, SYSUSERS U
           WHERE O.NAME = 'DTULTATU_TIPOSANGRIA' AND
                 TYPE   = 'TR' 	               AND
	              U.UID  = O.UID           AND
	              U.NAME = USER)
DROP TRIGGER DTULTATU_TIPOSANGRIA
GO

CREATE TRIGGER DTULTATU_TIPOSANGRIA
ON TIPOSANGRIA
FOR INSERT, UPDATE, DELETE
AS
BEGIN
   IF (0 = (SELECT COUNT(*) FROM INSERTED)) BEGIN
      INSERT INTO DELETECACHE (NMTABLECACHE, CDFILICACHE, DSKEYCACHE, DTULTATUCACH)
			      (SELECT 'TIPOSANGRIA', '', 'CDFILIAL='+CDFILIAL+';CDTPSANGRIA='+CDTPSANGRIA+';', GETDATE() FROM DELETED)
   END
   ELSE BEGIN
      UPDATE TIPOSANGRIA
      SET DTULTATU = GETDATE()
      FROM INSERTED
      WHERE (ISNULL(TIPOSANGRIA.CDFILIAL   ,0) = ISNULL(INSERTED.CDFILIAL   ,0)) AND
            (ISNULL(TIPOSANGRIA.CDTPSANGRIA,0) = ISNULL(INSERTED.CDTPSANGRIA,0));
   END;
END;
GO

-- VERSAO --
EXEC ATUVEROBJ 'DTULTATU_TIPOSANGRIA','5.07.005';
GO

