Place the DECLARE CURSOR Statement First
The DECLARE CURSOR statement must precede any other commands (such as OPEN, CLOSE, and FETCH) relating to the cursor because of the way the DB2 precompiler parses and extracts the SQL statements from the program.
The DECLARE CURSOR statement is not an executable statement and should not be coded in the PROCEDURE DIVISION of an application program. Although doing so does not cause a problem, it makes your program difficult to understand and could cause others to think that DECLARE is an executable statement.
You should place all cursor declarations in the WORKING-STORAGE section of the application program, immediately before PROCEDURE DIVISION. All host variable declarations must precede the DECLARE CURSOR statement in the application program.
If u declare the Cursor in procedure division, make sure that it comes before other commands (such as OPEN, CLOSE, and FETCH) both physically and logically because DB2 precompiler parses in coding sequence and execution takes place in logical sequence.