Q&A about the site for researchers and developers who explore the principles of a system through analysis of its structure, function, and operation
create table #temp ( id int, value int) insert into #temp values (1, 1) insert into #temp values (2, 2) insert into #temp values (3, 3) declare @sum int declare @biggest int select @biggest = max(id) from #temp while @biggest != 0 begin select @sum = @sum + value from #temp where id = @biggest select @biggest = @biggest - 1 select @sum end select @sum