below is the table and i want to populate the session id's in the table below. with the code i had written to do it. the session id are getting populated in in sequence.
Need some help with the SQL below Code
declare @maxRow as INT
declare @currentRow as INT
declare @sval as INT
set @maxRow = (select COUNT(*) from dbo.stage3 as count)
set @currentRow = 1
set @sval = (select case when (select max(sessionid) from dbo.fact) is null then 0 else max(sessionid) end from dbo.fact)
set @sval = @sval + 1
print @sval
while (@currentRow <= @maxRow)
begin
if @currentRow = 1
begin
update dbo.stage3
set sessionid = @sval
where number = @currentRow
end
else
begin
if
((select recordtype from dbo.stage3 where Number = @currentRow) = '4' and
(select seconds from dbo.stage3 where Number = @currentRow) > 30 and
((select previous_action from dbo.stage3 where Number = @currentRow) = '1' or
(select previous_action from dbo.stage3 where Number = @currentRow) = '5' ))
or
( (select isnull (PlayerCashableAmount,0) PlayerCashableAmount from dbo.stage3 where Number = @currentRow) < 0 and
(select seconds from dbo.stage3 where Number = @currentRow) < 30 and
((select next_action from dbo.stage3 where Number = @currentRow) = '1' or
(select next_action from dbo.stage3 where Number = @currentRow) = '4' ) )
or
(
((select recordtype from dbo.stage3 where Number = @currentRow) = '5' and
(select seconds from dbo.stage3 where Number = @currentRow) < 30 and
(select next_action from dbo.stage3 where Number = @currentRow) = '4' ) or
(select next_action from dbo.stage3 where Number = @currentRow) = '4'
)
begin
set @sval = @sval+1
update dbo.stage3 set sessionid = @sval where number = @currentRow
end
else
begin
set @sval = @sval+1
update dbo.stage3 set sessionid = @sval where number = @currentRow
end
set @sval = @sval
end
set @sval = @sval
print @sval
set @currentRow = @currentRow +1
print @currentRow
end
vltidprevious_vltidtransactionidrecordtypetransactiontimeplayercashableamountsecondsprevious_actionnext_actionsessionidnumber143876NULL30859052020-12-03 15:02:2101141NULL51114388014387620633852020-12-03 14:43:2001625925522144264143880127153352020-12-05 11:53:1201138055433144423144264401016642020-12-06 19:29:580115112544414442514442330493842020-12-05 11:31:2601569455514629914629788379652020-12-03 14:32:4102774154464314629914629989500142020-12-06 19:36:1609654951744146505146299129355712020-12-05 16:47:0832.9341845146505146505129355812020-12-05 16:47:1132.5211846146505146505129355912020-12-05 16:47:1332.1411847146505146505129356012020-12-05 16:47:1731.7411848146505146505129356112020-12-05 16:47:2032.1311849146505146505129356212020-12-05 16:47:2431.3311850146505146505129356312020-12-05 16:47:2731.3211851146505146505129356412020-12-05 16:47:2830.9311852146505146505129356512020-12-05 16:47:3230.5311853146505146505129356612020-12-05 16:47:3529.7211854146505146505129356712020-12-05 16:47:3629.7311855146505146505129356812020-12-05 16:47:3929.3311856146505146505129356912020-12-05 16:47:4328.9311857146505146505129357012020-12-05 16:47:4628.5411858146505146505129357112020-12-05 16:47:4928.51743315859