RPG programmers have gotten used to the idea of being able to define variables anywhere they like in a program. As long as you state the size and type of variable somewhere in the program, it will compile. It doesn't matter if the definition is physically after your first use of the variable. However, this concept could fail you when writing an RPG program with embedded SQL.
I was writing a report program that needed the three main SQL parts: DECLARE CURSOR, OPEN CURSOR, and FETCH. To make the mainline of the program less cluttered, I decided to put each of these three sections in separate subroutines and call them in the proper sequence.
The program would not compile. The error message in the compile listing told me that I could not OPEN a CURSOR without a DECLARE, but there was a DECLARE in the program. After a few minutes of head scratching, I found my problem. I had arranged my three subroutines in no particular order, and I did not realize that?unlike variable definitions?I had to have them in the proper physical order. After I rearranged the physical location of the subroutines in the proper DECLARE, OPEN, FETCH physical order, the program compiled.
When you compile an SQL RPG program, the compile command first runs an SQL precompiler that looks at the SQL part of the program. It appears that this precompiler is not as robust as the regular RPG compiler because it does not look ahead to verify that all the pieces are in the program.
? Kenneth Fordham
LATEST COMMENTS
MC Press Online