sqldb2jdedwards

Why doesn't this SQL work? [JD Edwards DB2]


I am attempting to write some SQL that will be used to select data for a report. The data is coming from JD Edwards, using the DB2 database on AS/400.

I have written the following SQL query:

SELECT F3112.WLDOCO AS DOC_NO,
       F3112.WLDCTO AS DOC_TYPE,
       F3112.WLCTS4 AS UDL_AMOUNT,
       F3112.WLCTS9 AS UDL_HOURS,
       F3112.WLLABA AS RUN_LABOR_ACT,
       F3112.WLMACA AS RUN_MACHINE_ACT,
       F0911.GLSBL  AS OBJ_ACCT,
       F0911.GLAA   AS GL_AMOUNT,
       F0911.GLU    AS GL_UNITS,
       F4801.WASRST AS WO_STATUS
FROM   PROD2DTA.F3112 F3112
       INNER JOIN PROD2DTA.F0911 F0911
         ON F3112.WLDOCO = CAST(F0911.GLSBL AS INTEGER)
       INNER JOIN PROD2DTA.F4801 F4801
         ON F3112.WLDOCO = F4801.WADOCO
WHERE  F0911.GLOBJ = '6855'
       AND F4801.WASRST BETWEEN '30' AND '95'

Where:

F3112 - Work Order Routing

F4801 - Work Order Master

F0911 - Account Ledger

This query will not run. It gives me a somewhat cryptic "data conversion or mapping error". I haven't been able to figure out what the offending SQL is.

Now here's the weird part. If I modify this query slighty to join F3112 with ONLY F4801 OR F0911, either form of the query works. I just can't get them to work together as one query (Is there a clue here?).

So, to illustrate:

SELECT F3112.WLDOCO AS DOC_NO,
       F3112.WLDCTO AS DOC_TYPE,
       F3112.WLCTS4 AS UDL_AMOUNT,
       F3112.WLCTS9 AS UDL_HOURS,
       F3112.WLLABA AS RUN_LABOR_ACT,
       F3112.WLMACA AS RUN_MACHINE_ACT,
       F0911.GLSBL AS OBJ_ACCT,
       F0911.GLAA   AS GL_AMOUNT,
       F0911.GLU    AS GL_UNITS
FROM   PROD2DTA.F3112 F3112
       INNER JOIN PROD2DTA.F0911 F0911
         ON F3112.WLDOCO = CAST(F0911.GLSBL AS INTEGER)
WHERE  F0911.GLOBJ = '6855'

and

SELECT F3112.WLDOCO AS DOC_NO,
       F3112.WLDCTO AS DOC_TYPE,
       F3112.WLCTS4 AS UDL_AMOUNT,
       F3112.WLCTS9 AS UDL_HOURS,
       F3112.WLLABA AS RUN_LABOR_ACT,
       F3112.WLMACA AS RUN_MACHINE_ACT,
       F4801.WASRST AS WO_STATUS
FROM   PROD2DTA.F3112 F3112
       INNER JOIN PROD2DTA.F4801 F4801
         ON F3112.WLDOCO = F4801.WADOCO
WHERE  F4801.WASRST BETWEEN '30' AND '95'

These both work just fine.


Solution

  • Thank you to @nfgl. The use of DIGITS was what fixed the "data conversion" issue.

    I tried both of the answer suggestions above (they worked once I used DIGITS instead of CAST) and with a little experimentation and tweaking, I basically get the same results in all cases.

    My SQL now looks like this:

    SELECT F3112.WLDOCO AS DOC_NO,
           F3112.WLDCTO AS DOC_TYPE,
           F3112.WLCTS4 AS UDL_AMOUNT,
           F3112.WLCTS9 AS UDL_HOURS,
           F3112.WLLABA AS RUN_LABOR_ACT,
           F3112.WLMACA AS RUN_MACHINE_ACT,
           F0911.GLSBL  AS OBJ_ACCT,
           F0911.GLAA   AS GL_AMOUNT,
           F0911.GLU    AS GL_UNITS,
           F4801.WASRST AS WO_STATUS
    FROM   PROD2DTA.F3112 F3112
           INNER JOIN PROD2DTA.F0911 F0911
             ON DIGITS(F3112.WLDOCO) = F0911.GLSBL
           INNER JOIN PROD2DTA.F4801 F4801
             ON F3112.WLDOCO = F4801.WADOCO
    WHERE  F0911.GLOBJ = '6855'
           AND F4801.WASRST BETWEEN '30' AND '95'
    

    The only difference from my original is the use of DIGITS in the ON condition of the first inner join.

    I think this gets me what I need. If anyone sees a problem, please let me know.