I am looking for the ways to reduce the looping for the below stored procedure. This takes around 30 minutes to loop around 30,000 records. Let me know if there are ways to rewrite the stored procedure.
DECLARE
@startloop numeric(9, 0),
@endloop numeric(9, 0),
@posloop numeric(9, 0),
@memid numeric(9, 0),
@groupid numeric(9, 0),
@billfreq char(1),
@recid numeric(10, 0),
@currptd datetime,
@joindate datetime,
@recdate datetime,
@a_dt_rundate datetime,
@recamt money,
@l_m_arrears money,
@l_m_advance money,
@l_m_arrears_no_other money,
@l_m_advance_no_other money,
@l_m_arrsub money,
@l_m_advsub money,
@l_m_arrreb money,
@l_m_advreb money,
@l_m_memcont money,
@l_m_arrlhc money,
@l_m_advlhc money,
@l_m_arroth money,
@l_m_advoth money,
@l_m_arrbreakup numeric(9, 0),
@l_m_advbreakup numeric(9, 0),
@l_m_arrpercent numeric(12, 8),
@l_m_advpercent numeric(12, 8),
@percentage numeric(12, 8),
@l_m_unarrears money,
@l_m_unarreb money,
@l_m_unarsub money,
@l_m_unarlhc money,
@l_m_unaroth money,
@l_m_unarbreakup money,
@l_m_cover_total money ,
@recamt_threshold money,
@rebate_tier_id int
SELECT
@recdate = NULL,
@currptd = NULL,
@recamt = NULL,
@l_m_advance = 0,
@l_m_arrears = 0,
@recid = NULL,
@l_m_memcont = 0,
@groupid = NULL,
@billfreq = NULL,
@l_m_arrsub = NULL,
@l_m_advsub = NULL,
@l_m_arrreb = NULL,
@l_m_advbreakup = NULL,
@l_m_arrbreakup = NULL,
@l_m_advreb = NULL,
@l_m_arrears_no_other = 0,
@l_m_advance_no_other = 0,
@l_m_unarrears = 0 ,
@l_m_unarreb = NULL,
@l_m_unarsub = NULL,
@l_m_unarlhc = NULL,
@l_m_unaroth = NULL,
@l_m_unarbreakup = NULL,
@recamt_threshold = 0,
@rebate_tier_id = null
SELECT @startloop = min(row_id), @endloop = max(row_id), @a_dt_rundate = dateadd(dd, -1, @l_dt_run)
FROM #members_arr
SELECT @posloop = @startloop
WHILE @posloop <= @endloop
BEGIN
/* Get member_id*/
SELECT @memid = #members_arr.membership_id, @currptd = date_paidto, @joindate = join_date, @groupid = group_id, @recid = rec_id
FROM #members_arr
WHERE row_id = @posloop
SELECT @recdate = dbo.rept.new_paid_to,
@recamt = dbo.rept.receipt_amount
FROM dbo.rept
WHERE dbo.rept.membership_id = @memid AND dbo.rept.rept_id = @recid
SELECT TOP 1 @rebate_tier_id = rebate_memship_tier.rebate_tier_id
FROM rebate_memship_tier
WHERE membership_id = @memid
ORDER BY effective_date DESC
-- Disable Receipt Proportionality Functionality
SELECT @recamt = null
IF (@recdate IS NULL OR @recdate < @joindate)
BEGIN
SELECT @recdate = @currptd
-- PRINT 'NULL'
END
IF @recdate < dateadd(dd, -1, @l_dt_run)
BEGIN
EXECUTE dbo.p_cal_rept_approx_ptd
@memid,
@recdate,
@a_dt_rundate,
'N',
@l_m_arrears OUTPUT,
@l_m_arrreb OUTPUT,
@l_m_arrsub OUTPUT,
@l_m_arrlhc OUTPUT,
@l_m_arroth OUTPUT,
@l_m_arrbreakup OUTPUT,
@percentage OUTPUT,
@rebate_tier_id
SELECT @l_m_arrears = @l_m_arrears + @l_m_arrreb + @l_m_arrsub
SELECT @l_m_arrears_no_other = @l_m_arrears - @l_m_arroth
END
SELECT @billfreq = dbo.billing_group.billing_freq
FROM dbo.billing_group
WHERE dbo.billing_group.group_id = @groupid
PRINT 'rebate tier id' + isnull(cast(@rebate_tier_id as varchar(max)),'[null]')
/* Get member's standard contribution*/
EXECUTE dbo.p_calc_nettfee_lhc
@memid,
NULL,
@l_dt_run,
@billfreq,
1,
@percentage,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
@l_m_cover_total OUTPUT,
NULL,
NULL,
@l_m_memcont OUTPUT,
NULL,
NULL,
NULL,
@rebate_tier_id
IF @l_m_arrears > 0
BEGIN
-- PRINT 'Record: ' + CAST(@posloop AS varchar(max)) + ' Member: ' + CAST(@memid AS varchar(max)) + ' Date Paid: ' + CAST(@recdate AS varchar(max)) + ' Cont: ' + CAST(@l_m_memcont AS varchar(max))+ ' '
SELECT @l_m_unarrears = 0.0
EXEC p_calc_arrears_unearned @recdate, @l_dt_run, @billfreq, @l_m_cover_total, @l_m_unarrears OUTPUT
END
ELSE
SELECT @l_m_unarrears = 0.0
/* Update #members_arr table with key data*/
UPDATE #members_arr
SET
date_paidto = @recdate,
member_arrears = @l_m_arrears,
member_advance = @l_m_advance,
member_arrears_no_other = @l_m_arrears_no_other,
member_advance_no_other = @l_m_advance_no_other,
member_cont = isnull(@l_m_memcont, 0),
member_arrears_unearned=@l_m_unarrears
WHERE row_id = @posloop
/* reset loop variables*/
SELECT
@recdate = NULL,
@recamt = NULL,
@currptd = NULL,
@l_m_advance = 0,
@l_m_arrears = 0,
@recid = NULL,
@l_m_memcont = 0,
@groupid = NULL,
@billfreq = NULL,
@l_m_arrsub = NULL,
@l_m_advsub = NULL,
@l_m_arrreb = NULL,
@l_m_advbreakup = NULL,
@l_m_arrbreakup = NULL,
@l_m_advreb = NULL,
@l_m_arrears_no_other = 0,
@l_m_advance_no_other = 0,
@rebate_tier_id = null
/* Increment counter*/
SELECT @posloop = @posloop + 1