OK, hãy thử cái này.
CREATE TABLE MyTable (Id INT Identity, Col2 int, Col3 int)
INSERT INTO MyTable (Col2, Col3)
VALUES (2,1), (3,0), (4,0),(5,0),(6,0)
SELECT * from MyTable
WHILE (SELECT COUNT(*) FROM MyTable WHERE Col3=0) > 0
BEGIN
UPDATE TOP (1) MyTable
SET CoL3 = (Mytable.col2 + (select col3 from mytable t2 where (t2.id = mytable.id-1)))
WHERE Col3 = 0
END
SELECT * from MyTable
Sử dụng WHILE
vòng lặp sẽ nhanh hơn con trỏ trong hầu hết các trường hợp.