IF EXISTS (SELECT * 
           FROM   sysobjects 
           WHERE  id = object_id(N'CT_Cbtes_Est_Hist') 
                  AND OBJECTPROPERTY(id, N'IsTrigger') = 1)
BEGIN
    DROP TRIGGER CT_Cbtes_Est_Hist
END
GO


CREATE TRIGGER CT_Cbtes_Est_Hist ON Cbtes
FOR UPDATE 
AS 
SET NOCOUNT ON

IF dbo.CF_xParam('User_Sql_Hab') = '1'
BEGIN
	INSERT INTO Cbtes_Estados_Hist (Id_Trans,Fecha,Id_Estado_Cbte,Id_Usuario) 
		SELECT I.Id_Trans, GETDATE(),D.Id_Estado_Cbte , CASE  ISNUMERIC(SYSTEM_USER) WHEN 1 THEN SYSTEM_USER ELSE 0 END
				FROM Deleted D, Inserted I
				WHERE I.Id_Trans = D.Id_Trans
				AND I.Id_Estado_Cbte <> D.Id_Estado_Cbte
END

