Cobol-DB2 Cursor Overview

Db2-Cursor:

Cursors in the application program are used to retrieve the multiple rows and process them one by one .

Generally by using a singleton select statement in the application program, it is possible to retrieve only one row at a time as the host variable structure allows keeping one value at a time.

Ex:

By using the above statement we can only have one set values at one execution of the program.

But it is quite common to have a requirement to retrieve a set of rows that meets particular criteria and process all the set of rows one by one. This can be achieved by the mechanism called CURSOR.

Using Cursors in application programs is quite simple and easy to understand.

Using Cursor in application Programs:

Steps:

1) Declare the Cursor

2) Open it

3) Fetch the cursor

4) Close the cursor After you have processed all the required data

Declare Cursor:

This statement defines the cursor and gives a name to the Cursor. Basically it contains the SQL statement to be executed.

Sample Declare Cursor Statement:

Notes:

  1. Declare Cursor statement is not an executable one, it just specifies the basic SQl to be executed and gives a name to the cursor.
  2. This can be declared either in Working storage or in procedure division.

Open Cursor:

Open cursor statement is an executable one. Once it has been issued, the SQl statement gets executed and a result set will be built and all the rows retrieved will be put in the result set.

The cursor will be pointed to the first row in the result set, so it is ready to process the rows.

Ex:

Fetch:

Fetch statement will fetches one set of values at a time in to the host variables, and this can be kept in loop to process all the rows until no more rows in the result set.

Ex:

The above topic is about the basics of cursors in cobol db2 application program.

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami