DECLARE @C1 as int
DECLARE @C2 as int
DECLARE @C3 as float
DECLARE @C4 as float
DECLARE @LAST_C1 as int
DECLARE @LAST_C4 as float
SET @LAST_C1 = NULL
DECLARE cp_run CURSOR
FOR SELECT C1, C2, C3, C4 FROM cp
OPEN cp_run
FETCH NEXT FROM cp_run
INTO @C1, @C2, @C3, @C4
WHILE @@FETCH_STATUS = 0
BEGIN
IF(NOT @LAST_C1 IS NULL)
BEGIN
SELECT @LAST_C4 = C4 FROM CP WHERE C1 = @LAST_C1
UPDATE cp SET C3 = (@LAST_C4 * @C2) / 365 WHERE C1 = @C1
UPDATE cp SET C4 = @LAST_C4 + ((@LAST_C4 * @C2) / 365) WHERE C1 = @C1
END
SET @LAST_C1 = @C1
FETCH NEXT FROM cp_run
INTO @C1, @C2, @C3, @C4
END
CLOSE cp_run
DEALLOCATE cp_run
SELECT * FROM cp