am reaching out for assistance regarding a persistent SQL error I am encountering on an AS400 DB2 database when using the CASE statement within a stored procedure.
My environment details are as follows:
- Database: IBM DB2 for iSeries
- Client Tool: DBeaver
The issue arises specifically when I attempt to incorporate a CASE statement into my SELECT query within the stored procedure. The SELECT query runs flawlessly on its own, but once embedded in the stored procedure with the CASE logic, it fails.
Here is the stored procedure that triggers the error:
CREATE PROCEDURE ZRDTA9999.GetMStop(in nanoParam CHAR(3))
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
DECLARE c1 CURSOR WITH RETURN FOR
SELECT
RTRIM(LTRIM(NH.X1NANO)) AS National_Account_Number,
RTRIM(LTRIM(NM.X7CMP)) AS Plant,
RTRIM(LTRIM(LN.X2LNAM)) AS Location,
RTRIM(LTRIM(NM.X7MSTP)) AS Master_Stop,
RTRIM(LTRIM(TM.AYMSNM)) AS Company_Name,
RTRIM(LTRIM(TM.AYORDR)) AS PO_Ref,
RTRIM(LTRIM(TM.AYBCHI)) AS Inv_Cycle,
CASE
WHEN (SELECT COUNT(1)
FROM TRDTA2401.TRSCTL AS TC
WHERE TC.ALMSTP = TM.AYMSTP
AND TC.ALCMP = TM.AYCMP
AND TC.ALSSTS IN ('1', '2', '3')) > 0 THEN 'Y'
ELSE 'N'
END AS Active
FROM
xxxx.NAHEAD AS NH
JOIN
xxxx.NAMSTP AS NM ON NH.X1NANO = NM.X7NANO
JOIN
xxxx.NALOCN AS LN ON NM.X7CMP = LN.X2CMP
JOIN
tttt.TRMSTP AS TM ON NM.X7MSTP = TM.AYMSTP
WHERE
NH.X1NANO = nanoParam;
OPEN c1;
END;
The error message displayed is as follows:
SQL Error [42601]: [SQL0104] Token <END-OF-STATEMENT> was not valid. Valid tokens: ;.
It's important to note that the SELECT statement within the procedure executes without errors when run independently of the stored procedure.
I have tried various modifications to the script without success:(
Thank you for your assistance.
------------------------------
Flora Mousavi
------------------------------