Joined: 20 Oct 2006 Posts: 6970 Location: porcelain throne
if you look at the pre-compiler listing, you will notice that a declare:
sets up the variables used by db2
inserts cobol code between your PROCEDURE DIVISION USING statement and the first line of your code which establishes addressability with db2 and your program. This code (unless you internally modify the 'flag') is only executed once - at the start of execution of your program - unless you issue a cancel for your program from a CALLing module.
FETCH is a db2 term used to define the process of populating your host variables (defined in the DECLARE CURSOR) with values obtained from the result table, which is built as a result of the OPEN Cursor sql statement. The result table contains values from rows/colums selected from Table T during the OPEN processing.
The rows of the result table can be derived during the execution of the OPEN statement and a temporary copy of a result table can be created to hold those rows. They can be derived during the execution of later FETCH statements. In either case, the cursor is placed in the open state and positioned before the first row of its result table. If the table is empty, the position of the cursor is effectively “after the last row.” The DB2 system does not indicate an empty table when the OPEN statement is
I got the above information from DB2 SQL reference --> Chapter 5. OPEN statement.