I am trying to run the below cursor to sequentially number the records for each "rectype" in a transaction each starting at 1. The problem is this took 35 minutes to run on my sample of 46,000 records. I could potentially have over a million at a time in production.
Thank You in advance for your help!!!
Thank You in advance for your help!!!
Code:
declare @transdate smalldatetime
declare @storenum int
declare @regnum int
declare @transnum int
declare @rectype char(3)
declare @vn_loop_count int
declare @vn_rowcount int
declare c_trans_info cursor for
select distinct
transdate
,storenum
,regnum
,transnum
,rectype
from info_report
order by 1, 2, 3, 4, 5
open c_trans_info
fetch c_trans_info into @transdate, @storenum, @regnum, @transnum, @rectype
while ( @@fetch_status <> -1 )
begin
set @vn_loop_count = 1
set @vn_rowcount = 1
set rowcount 1
while @vn_rowcount = 1
begin
update info_report
set rectype_id = @vn_loop_count
where transdate = @transdate
and storenum = @storenum
and regnum = @regnum
and transnum = @transnum
and rectype = @rectype
and rectype_id is null
select @vn_rowcount = @@rowcount
select @vn_loop_count = @vn_loop_count + 1
end
set rowcount 0
fetch c_trans_info into @transdate, @storenum, @regnum, @transnum, @rectype
end
deallocate c_trans_info
set rowcount 0