DB22层循环跳出问题
本人菜鸟一枚,存储过程中有2层嵌套循环,其中需要在第二层循环中有条件跳出。只用过for 循环,大概知道要换成while或者loop方式,需要用到游标,可是不知道怎么把第一层的循环结果赋值给第二层,故在此请问各位大侠帮帮忙!~不甚感激!
过程如下:
CREATE OR REPLACE PROCEDURE "TJBIZT"."PROC_TEST" (
IN "AS_BOOK_CODE" VARCHAR(4),
IN "V_PERIOD_NO" VARCHAR(6) )
SPECIFIC "SQL141014152341803"
LANGUAGE SQL
NOT DETERMINISTIC
EXTERNAL ACTION
MODIFIES SQL DATA
OLD SAVEPOINT LEVEL
BEGIN
DECLARE i_subject_id VARCHAR(10);
DECLARE LV_AMOUNT DOUBLE;
DECLARE lv_acct_title_code VARCHAR(20);
DECLARE lv_id_code VARCHAR(20);
DECLARE acc_dept_code VARCHAR(20) ;
SET acc_dept_code = 'BGHDA002';
SET i_subject_id = '1122';
--先进先出法,以最小期间的借方数抵消最大期间的贷方数
--借方负数等同与贷方正数
FOR C1 AS SELECT *
FROM TJBIZT.T_TT_CREDIT_AGE_new t
WHERE t.acct_period_no = V_PERIOD_NO
AND t.company_code = AS_BOOK_CODE
AND substr (t.acct_title_code, 1, 4) = i_subject_id
AND t.ref_code = acc_dept_code
and t.id_code='R09312'
AND t.acct_age_amt < 0
ORDER BY t.period_age DESC,
t.acct_title_code ASC,
t.id_code ASC,
t.currency_code ASC,
t.ref_code ASC
DO
SET lv_amount = 0,
lv_acct_title_code = c1.acct_title_code,
lv_id_code = c1.id_code,
lv_amount = c1.acct_age_amt;
FOR C2 AS SELECT *
FROM TJBIZT.T_TT_CREDIT_AGE_new t
WHERE t.acct_period_no = V_PERIOD_NO
AND t.company_code = AS_BOOK_CODE
AND t.ref_code = c1.ref_code
AND t.acct_title_code = c1.acct_title_code
AND t.id_code = c1.id_code
AND t.currency_code = c1.currency_code
AND t.acct_age_amt > 0
ORDER BY t.period_age ASC,
t.acct_title_code ASC,
t.id_code ASC,
t.currency_code ASC,
t.ref_code ASC
DO
SET lv_amount = c2.acct_age_amt + lv_amount;
IF lv_amount >= 0
THEN
UPDATE TJBIZT.T_TT_CREDIT_AGE_NEW t
SET t.acct_age_amt = lv_amount
WHERE t.acct_period_no = V_PERIOD_NO
AND t.company_code = AS_BOOK_CODE
AND t.ref_code = c1.ref_code
AND t.acct_title_code = c1.acct_title_code
AND t.id_code = c1.id_code
AND t.period_age = c2.period_age
AND t.currency_code = c1.currency_code
AND t.acct_amt_mark = '1';
DELETE TJBIZT.T_TT_CREDIT_AGE_NEW t
WHERE t.acct_period_no = V_PERIOD_NO
AND t.company_code = AS_BOOK_CODE
AND t.ref_code = c1.ref_code
AND t.acct_title_code = c1.acct_title_code
AND t.id_code = c1.id_code
AND t.period_age = c1.period_age
AND t.currency_code = c1.currency_code
AND t.acct_amt_mark = '-1';
----此处想要跳出C2循环;
ELSE
DELETE TJBIZT.T_TT_CREDIT_AGE_NEW t
WHERE t.acct_period_no = V_PERIOD_NO
AND t.company_code = AS_BOOK_CODE
AND t.ref_code = c1.ref_code
AND t.acct_title_code = c1.acct_title_code
AND t.id_code = c1.id_code
AND t.period_age = c2.period_age
AND t.currency_code = c1.currency_code
AND t.acct_amt_mark = '1';
END IF;
END FOR;
DELETE TJBIZT.T_TT_CREDIT_AGE_NEW t
WHERE t.acct_period_no = V_PERIOD_NO
AND t.company_code = AS_BOOK_CODE
AND t.ref_code = c1.ref_code
AND t.acct_title_code = c1.acct_title_code
AND t.id_code = c1.id_code
AND t.period_age = c1.period_age
AND t.currency_code = c1.currency_code
AND t.acct_amt_mark = '-1';
END FOR;
END;