DB22层循环跳出问题

applelulu 2014-10-15 05:21:41
本人菜鸟一枚,存储过程中有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;
...全文
204 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2014-10-27
  • 打赏
  • 举报
回复
用公用表达式也许可以。
applelulu 2014-10-15
  • 打赏
  • 举报
回复
谢谢楼上答复,是从oracle存储过程翻过来的。因为第二层循环需要用到第一层循环的结果作为查询条件,我想也许可以分成2个存储过程试试
  • 打赏
  • 举报
回复
你要实现怎样的业务,一般一层循环就够了或者把存储过程分成多个,每个存储过程只负责相应的业务!

5,889

社区成员

发帖
与我相关
我的任务
社区描述
IBM DB2 是美国IBM公司开发的一套关系型数据库管理系统,它主要的运行环境为UNIX(包括IBM自家的AIX)、Linux、IBM i(旧称OS/400)、z/OS,以及Windows服务器版本
社区管理员
  • DB2
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧