
Larry C. answered 08/07/19
Computer Science and Mathematics professional
Any time you have a relational database query (DB2 or otherwise) embedded in an application that can return more than 1 row, you need to capture the resultset in what is (unfortunately, imo) called a cursor. This is because COBOL (like most other programming languages) is not equipped to process multiple records at the same time. The process works something like this:
1) Define the cursor - basically lay out the SQL and assign the cursor a name. This is done in working storage. The remaining steps occur in the Procedure Division.
2) Open the cursor - this actually executes the SQL and populates a section of memory allocated to store the rows of the resultset. A pointer is initialized to point to the first row.
3) Within a loop, execute a fetch. This gets the row of the resultset the pointer currently points to and populates a collection of specified data fields to hold the various columns of the row where they are now available for whatever process is to use the data. The pointer is then moved to the next row. If there were no more rows to process, the loop is exited.
4) Close the cursor - this frees up the allocated memory for the resultset.