我在 db2 i 7.3 中创建过程时遇到问题。主要的想法是给定的结束日期(应该是今天(将值插入到所选结果与fist_day(结束日期 - 3 年(和last_day(结束日期 - 3 年(匹配的特定表中,直到今天。
当前返回此错误: 14:19:25 [创建 - 0 行, 0.000 秒] [错误代码: -199, SQL 状态: 42601] [SQL0199] Palavra-chave FOR não esperada.Testemunhos válidos: ;. ...执行 1 条语句,0 行受到影响,执行/获取时间:0.000/0.000 秒 [0 成功、0 警告、1 错误]在我的代码上方:
CREATE PROCEDURE MYLIB.SP_DIFJURO ()
LANGUAGE SQL
MODIFIES SQL DATA
BEGIN
DECLARE DATAJURO DATE DEFAULT NULL;
FOR DATAJURO > (SELECT CURRENT_DATE - 3 YEAR FROM SYSIBM.SYSDUMMY1)
DO
INSERT INTO MYLIB.SDDIFJUROD (CONTAJURO, JUROCALC, JURODEBIT, DATAINICIO, DATAFIM)
SELECT NCJCCONTA, totaljurocalculado, NCLJVLRJ, LEFT(CHAR(LAST_DAY(DATAJURO), ISO),8)||'01', LAST_DAY(DATAJURO) FROM ( SELECT SUM(valorjurocalculado) totaljurocalculado, a.NCJCCONTA from
(SELECT C.NCJCCONTA NCJCCONTA, C.NCJCDTPRO, C.NCJCDTJUR,
CASE
WHEN R.NCJCTREG = 'R' THEN R.NCJCSLDV
ELSE C.NCJCSLDV
END saldodatavalor,
CASE
WHEN R.NCJCTREG = 'R' THEN round(R.NCJCTXC,2)
ELSE round(C.NCJCTXC,2)
END taxadiaria, decimal(round((ifnull(R.NCJCSLDV, C.NCJCSLDV)*ifnull(R.NCJCTXC, C.NCJCTXC)/100)/360,2),15,2) valorjurocalculado
FROM (
SELECT NCJCCONTA, NCJCDTPRO, NCJCDTJUR, NCJCTREG, NCJCSLDV, NCJCMOED, NCJCTXC FROM MYLIB.LOGJR
WHERE NCJCDTJUR BETWEEN LEFT(VARCHAR_FORMAT(LAST_DAY(DATAJURO), 'YYYYMMDD'),6)||'01' AND VARCHAR_FORMAT(LAST_DAY(DATAJURO), 'YYYYMMDD')
AND NCJCCLCP = 'DO' AND NCJCTREG = 'C' AND NCJCNUMVD < 0 ) C
LEFT JOIN
(SELECT NCJCCONTA, NCJCDTPRO, NCJCDTJUR, NCJCTREG, NCJCSLDV, NCJCMOED, NCJCTXC FROM MYLIB.LOGJR
WHERE NCJCDTJUR BETWEEN LEFT(VARCHAR_FORMAT(LAST_DAY(DATAJURO), 'YYYYMMDD'),6)||'01' AND VARCHAR_FORMAT(LAST_DAY(DATAJURO), 'YYYYMMDD')
AND NCJCCLCP = 'DO' AND NCJCTREG = 'R' AND NCJCNUMVD < 0) R
ON C.NCJCCONTA = R.NCJCCONTA AND C.NCJCDTJUR = R.NCJCDTJUR
) a group by a.NCJCCONTA
)jurocalc
INNER JOIN MYLIB.LJRDC ON NCLJCONTA = NCJCCONTA
AND LEFT(NCLJBUFFER, 16) = LEFT(VARCHAR_FORMAT(LAST_DAY(DATAJURO), 'YYYYMMDD'),6)||'01'||VARCHAR_FORMAT(LAST_DAY(DATAJURO), 'YYYYMMDD')
WHERE int(ABS(totaljurocalculado)) != int(ABS(NCLJVLRJ));
SET DATAJURO = (SELECT DATAJURO - 1 MONTH FROM SYSIBM.SYSDUMMY1);
END FOR;
END
错误地使用了FOR
语句。
SQL/PL 中的FOR
看起来不像for loop
中的 C 或 Java 等编程语言。必须将SELECT
语句与FOR
一起使用,该语句将迭代此SELECT
的结果集,使您能够按游标名称引用FOR
正文内语句中的相应列值C1
。
在下面的示例中,使用递归公用表表达式的SELECT
语句返回从CURRENT_DATE - 3 YEARS
到CURRENT_DATE
的每个月的最后一天。您可以独立运行此SELECT
以检查/稍微修改其条件以返回所需日期的确切列表。
请注意,您不使用SET
语句来递增"循环变量"C1.DATAJURO
,因为SELECT
语句处理此问题。
FOR C1 AS
WITH DATES (DT) AS
(
VALUES FIRST_DAY(CURRENT_DATE)
UNION ALL
SELECT DT - 1 MONTH
FROM DATES
WHERE DT > CURRENT_DATE - 3 YEAR
)
SELECT LAST_DAY(DT) AS DATAJURO
FROM DATES
DO
INSERT INTO MYLIB.SDDIFJUROD ...
... C1.DATAJURO ...
END FOR;