IF EXISTS ( SELECT * FROM SYS.OBJECTS WHERE TYPE = 'TR' 
              AND name = 'CT_Cajas_Mov_Hist_Upd')
BEGIN
    DROP TRIGGER dbo.CT_Cajas_Mov_Hist_Upd
END
GO



CREATE TRIGGER [dbo].[CT_Cajas_Mov_Hist_Upd] ON [dbo].[Cajas_Mov]
AFTER UPDATE
AS 
SET NOCOUNT ON
DECLARE @Id_Trans					INT
DECLARE @Renglon_Nro	    		SMALLINT
DECLARE @Flag_Grabar				INT
DECLARE @Id_Periodo_Caja     		INT
Declare @Descripcion                VARCHAR(80)
DECLARE @Id_Usuario					SMALLINT


	IF ISNUMERIC(SYSTEM_USER) = 1
		SET @Id_Usuario =  SYSTEM_USER 
      ELSE
		SET @Id_Usuario =  0


	DECLARE [Cajas_Mov_Hist_update] CURSOR FOR
	SELECT i.Id_Trans,i.Renglon_Nro,D.Id_Periodo_Caja
			FROM Inserted I, Deleted D
			WHERE I.Id_Trans = D.Id_Trans
			AND (I.Id_Periodo_Caja <> D.Id_Periodo_Caja)
	

	OPEN [Cajas_Mov_Hist_update]
	FETCH NEXT FROM [Cajas_Mov_Hist_update] INTO @Id_Trans, @Renglon_Nro, @Id_periodo_Caja
            
	WHILE (@@FETCH_STATUS = 0)
	BEGIN
		INSERT INTO Cajas_Mov_Hist	   (Id_Trans,	     	Renglon_Nro,	Id_periodo_Caja,
										Fecha,              Descripcion,    Id_Usuario)
			VALUES					   (@Id_Trans ,		    @Renglon_Nro,	@Id_periodo_Caja,
										GETDATE(),	     	@Descripcion,	@Id_Usuario)


	
		FETCH NEXT FROM [Cajas_Mov_Hist_update] INTO @Id_Trans, @Renglon_Nro, @Id_periodo_Caja
								 
	END

	CLOSE [Cajas_Mov_Hist_update]
	DEALLOCATE [Cajas_Mov_Hist_update]
 