Bonediggler1
Technical User
Hi-
I am trying to run nested loops, but it seems as though once the inner loop is done the whole thing terminates...rather than going back to the outer loop.
Below is the code:
WHILE @COUNTER > -13
BEGIN
SET @ACCTGID = AR_DASHBOARD.FN_ACCTPER(@ACCTGID1, @COUNTER)
WHILE @COUNTER2 < 3
BEGIN
INSERT INTO AR_DASHBOARD.HISTORICAL_DSO (PRODUCT, ACCTG_PERIOD_ID, TOTAL_AR, AR_OVER_90, SUM_REVENUE, DAYS)
SELECT (SELECT PRODUCT FROM #TEMP_PROD WHERE ROWNUM = @COUNTER2),
@ACCTGID,
(SELECT SUM(BAL) FROM AR_DASHBOARD.AR_OPEN WHERE GL_PRODUCT = (SELECT PRODUCT FROM #TEMP_PROD WHERE ROWNUM = @COUNTER2) AND ACCTG_PERIOD_ID = @ACCTGID),
(SELECT SUM(CASE WHEN DBO.FN_BUCKETMONTH(AGNG_DT, @ACCTGID) > '4' THEN BAL ELSE 0 END) FROM AR_DASHBOARD.AR_OPEN WHERE GL_PRODUCT = (SELECT PRODUCT FROM #TEMP_PROD WHERE ROWNUM = @COUNTER2) AND ACCTG_PERIOD_ID = @ACCTGID),
(SELECT SUM(TRNS_AMNT) FROM AR_DASHBOARD.AR_REVENUE WHERE GL_PRODUCT = (SELECT PRODUCT FROM #TEMP_PROD WHERE ROWNUM = @COUNTER2) AND ACCTG_PERIOD_ID = @ACCTGID),
(SELECT CASE WHEN RIGHT(@ACCTGID, 2) IN ('01','05','07','08','09','10', '12') THEN '92'
WHEN RIGHT(@ACCTGID, 2) IN ('02', '03') THEN '90'
WHEN RIGHT(@ACCTGID, 2) IN ('04') THEN '89'
WHEN RIGHT(@ACCTGID, 2) IN ('06', '11') THEN '91'
END)
SET @COUNTER2 = @COUNTER2 + 1
END
SET @COUNTER = @COUNTER -1
END
Thank you!!
I am trying to run nested loops, but it seems as though once the inner loop is done the whole thing terminates...rather than going back to the outer loop.
Below is the code:
WHILE @COUNTER > -13
BEGIN
SET @ACCTGID = AR_DASHBOARD.FN_ACCTPER(@ACCTGID1, @COUNTER)
WHILE @COUNTER2 < 3
BEGIN
INSERT INTO AR_DASHBOARD.HISTORICAL_DSO (PRODUCT, ACCTG_PERIOD_ID, TOTAL_AR, AR_OVER_90, SUM_REVENUE, DAYS)
SELECT (SELECT PRODUCT FROM #TEMP_PROD WHERE ROWNUM = @COUNTER2),
@ACCTGID,
(SELECT SUM(BAL) FROM AR_DASHBOARD.AR_OPEN WHERE GL_PRODUCT = (SELECT PRODUCT FROM #TEMP_PROD WHERE ROWNUM = @COUNTER2) AND ACCTG_PERIOD_ID = @ACCTGID),
(SELECT SUM(CASE WHEN DBO.FN_BUCKETMONTH(AGNG_DT, @ACCTGID) > '4' THEN BAL ELSE 0 END) FROM AR_DASHBOARD.AR_OPEN WHERE GL_PRODUCT = (SELECT PRODUCT FROM #TEMP_PROD WHERE ROWNUM = @COUNTER2) AND ACCTG_PERIOD_ID = @ACCTGID),
(SELECT SUM(TRNS_AMNT) FROM AR_DASHBOARD.AR_REVENUE WHERE GL_PRODUCT = (SELECT PRODUCT FROM #TEMP_PROD WHERE ROWNUM = @COUNTER2) AND ACCTG_PERIOD_ID = @ACCTGID),
(SELECT CASE WHEN RIGHT(@ACCTGID, 2) IN ('01','05','07','08','09','10', '12') THEN '92'
WHEN RIGHT(@ACCTGID, 2) IN ('02', '03') THEN '90'
WHEN RIGHT(@ACCTGID, 2) IN ('04') THEN '89'
WHEN RIGHT(@ACCTGID, 2) IN ('06', '11') THEN '91'
END)
SET @COUNTER2 = @COUNTER2 + 1
END
SET @COUNTER = @COUNTER -1
END
Thank you!!