       DECLARE @CodClie    varchar(10)
       DECLARE @ActClie    varchar(10)
       DECLARE @Monto      decimal(13,2)
       DECLARE @NroUnico   INT
       DECLARE @Saldo      decimal(13,2)
       DECLARE @FECHAE     datetime
       DECLARE @NumeroD    varchar(10)
       DECLARE @TipoCxC    varchar(2)
       DECLARE @Saldoact   decimal(13,2)
       DECLARE @FACTOR     SMALLINT
       DECLARE @Saldoant   DECIMAL(13,2)
       DECLARE MCLIE CURSOR FOR
       SELECT DISTINCT A.CodClie 
       FROM SAClie A, SAACxC as B
       Where A.CodClie = B.CodClie
       Order By A.CodClie
       OPEN MCLIE
       FETCH NEXT FROM MCLIE INTO @ACTClie
       	WHILE @@FETCH_STATUS=0 BEGIN
       	   SET @SALDOANT=0
                  DECLARE MCURSOR CURSOR FOR
                  SELECT A.CodClie, A.FechaE, A.NroUnico, A.NumeroD,
                         A.TipoCxC, A.Monto,  A.Saldo,    A.Saldoact
                  FROM SAACxC as A
       	   Where A.CodClie = @ACTClie
                  ORDER BY CodClie, A.FechaE, A.NroUnico
                  OPEN Mcursor
                  FETCH NEXT FROM Mcursor INTO @CodClie, @FECHAE, @NroUnico, @NumeroD,
                                            @TipoCxC, @Monto,  @Saldo,    @Saldoact
                  WHILE (@@FETCH_STATUS<>-1)
                   BEGIN
                     IF (@@FETCH_STATUS<>-2)
                        BEGIN
                          SET @FACTOR=CASE SUBSTRING(@TIPOCxC,1,1)
                                       WHEN '1' THEN 1
                                       WHEN '2' THEN 1
                                       WHEN '3' THEN -1
                                       WHEN '4' THEN -1
                                       WHEN '5' THEN -1
                                       WHEN '6' THEN 1
                                       WHEN '7' THEN 1
                                       WHEN '8' THEN -1  
                                      END
                          IF (@TIPOCxC<>'50')
                             SET @SALDOANT=@SALDOANT+@FACTOR*@MONTO
                          UPDATE SAACxC
                          SET SALDOACT = @saldoant
                          WHERE NroUnico = @NroUnico
                        END
                     FETCH NEXT
                     FROM Mcursor
                     INTO @CodClie, @FECHAE, @NroUnico, @NumeroD,
                          @TipoCxC, @Monto,  @Saldo, @Saldoact
                   END
                  CLOSE MCursor
                  DEALLOCATE MCursor
       	   FETCH NEXT FROM MCLIE INTO @ACTClie
       END
       CLOSE MCLIE
       DEALLOCATE MCLIE
