websphereextended-sql

Can I do CAST reference in ESQL?


While reproducing an example from IBM documentation: https://www.ibm.com/support/knowledgecenter/SSMKHH_9.0.0/com.ibm.etools.mft.doc/ak04864_.htm with the following example message: https://www.ibm.com/support/knowledgecenter/SSMKHH_9.0.0/com.ibm.etools.mft.doc/ak05911_.htm without application of the message model, on: IBM Integration Toolkit Version: 10.0.0.16 with the following code fragment:


    CALL CopyEntireMessage();
    -- Declare the dynamic reference
    DECLARE myref REFERENCE TO OutputRoot.XMLNSC.Invoice.Purchases.Item[1];
    -- Continue processing for each item in the array
    WHILE LASTMOVE(myref)=TRUE
        DO
        -- Add 1 to each item in the array
        SET myref = CAST(myref AS INTEGER) + 1; 
        -- Move the dynamic reference to the next item in the array
        MOVE myref NEXTSIBLING;
    END WHILE;

I suddenly found out that the following line of code:

SET myref = myref + 1;

or alternatively:

SET myref = CAST(myref AS INTEGER) + 1; 

did not have any effect on the value in the first item, and, even more, it was preventing expected work of:

MOVE myref NEXTSIBLING;

so that the myref pointer did not move to the next sibling (did not move from the item[1] to the item[2]) and myref was just disappearing from the list of variables in my debug view.

My question:

Any idea why SET myref = myref + 1; or SET myref = CAST(myref AS INTEGER) + 1; do not work? According to the document, the latter should work without the message model.


Solution

  • I cannot explain the symptoms, but I can make a couple of helpful suggestions:

    1. Don't use counted loops in ESQL. It is almost always better to use a FOR loop to iterate over an array.
    CALL CopyEntireMessage();
        -- For each item in the array...
        FOR refItem AS OutputRoot.XMLNSC.Invoice.Purchases.Item[] DO
            SET refItem = CAST(refItem AS INTEGER) + 1; 
        END FOR;
    

    No need to declare the reference variable (but you can if you want to, to stop the ESQL editor from complaining about the 'undeclared' reference variable). No need to move the reference variable yourself - the FOR loop does it for you.

    1. If you really want to know why your WHILE loop is not working...

    You will find that the debugger only tells you what is happening. But (as you are finding out) it cannot tell you why it is happening. For that, you need a user trace. Not a Trace node, a user trace. You have to open the IIB console and use the mqsichangetrace, mqsireadlog, mqsiformatlog commands (in that order) to start user trace, read it and format it as text. It's a bit of hassle the first couple of times, but it will almost certainly show you why your code is not working.