Código SQL:
Ver originalCREATE TABLE #temp
(
dato1 INT,
dato2 INT,
dato3 INT
)
INSERT INTO #temp VALUES (323, 308, 631)
INSERT INTO #temp VALUES (0, 0, 0)
INSERT INTO #temp VALUES (0 ,1, 0)
INSERT INTO #temp VALUES (0 ,9, 0)
INSERT INTO #temp VALUES (0 ,1, 0)
INSERT INTO #temp VALUES (0 ,3, 0)
DECLARE @x INT
DECLARE @anterior INT
DECLARE @NEXT INT
DECLARE @valor INT
CREATE TABLE #temp3
(
dato1 INT,
dato2 INT,
dato3 INT,
rn INT
)
SET @x=1
SELECT *,IDENTITY(INT,1,1) rn INTO #temp2 FROM #temp
while @x<=(SELECT COUNT(*) FROM #temp2)
BEGIN
IF @x=1
BEGIN
INSERT INTO #temp3
SELECT * FROM #temp2 WHERE rn=@x
END
ELSE
BEGIN
SELECT @anterior=dato3 FROM #temp3 WHERE rn=@x-1
SELECT @valor=dato2 FROM #temp2 WHERE rn=@x
SET @NEXT=@anterior+@valor
INSERT INTO #temp3 VALUES (@anterior,@valor,@NEXT,@x)
END
SET @x=@x+1
END
SELECT * FROM #temp3
DROP TABLE #temp2
DROP TABLE #temp3
esto te sirve??