Technical Article

Running Total without using of Cursor

,

Script for computing  Running Total on Table without using Cursors.

--- Creaet Table ---
create table prsd_ss
(id1 int identity,
 val int,
 run_tot int null)

--- Populate Records
insert prsd_ss(val) select 100
insert prsd_ss(val) select 200
insert prsd_ss(val) select 300
insert prsd_ss(val) select 400
insert prsd_ss(val) select 500
---- Output
select * from prsd_ss
id1         val         run_tot     
----------- ----------- ----------- 
1           100         NULL
2           200         NULL
3           300         NULL
4           400         NULL
5           500         NULL

(5 row(s) affected)

----- ## Arriving of Running Total Without using of Cursor ##--

update prsd_ss
set run_tot = val 

declare @variable int
set @variable = 0
update  prsd_ss
SET @variable = run_tot = @variable + run_tot
---- Output
select * from prsd_ss

id1         val         run_tot     
----------- ----------- ----------- 
1           100         100
2           200         300
3           300         600
4           400         1000
5           500         1500

(5 row(s) affected)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating