FETCH

The FETCH statement positions a cursor on a row of its result table. It can return zero or one and assigns the values of the rows to host variables if there is a target specification.

Invocation

This statement can be embedded only in a COBOL application program. It is an executable statement that cannot be dynamically prepared.

FETCH syntax

Read syntax diagramSkip visual syntax diagramFETCHcursor-namesingle-row-fetch
Notes:

single-row-fetch syntax

Read syntax diagramSkip visual syntax diagramINTO, host-variableINTO DESCRIPTOR descriptor-name

Description

The following keyword parameters are defined for the FETCH statement:
INTO host-variable
Specifies a list of host variables. Each host-variable must identify a structure or variable that is described in the application program in accordance with the rules for declaring host structures and variables. A reference to a structure is replaced by a reference to each of its variables. The first value in the result row is assigned to the first host variable, the second value to the second host variable, and so on.
INTO DESCRIPTOR descriptor-name
Identifies an SQLIMSDA that contains a valid description of the host output variables. Result values from the associated SELECT statement are returned to the application program in the output host variables.

Before the FETCH statement is processed, you must set the following fields in the SQLIMSDA:

  • SQLIMSN to indicate the number of SQLIMSVAR occurrences provided in the SQIMSLDA
  • SQLIMSABC to indicate the number of bytes of storage allocated in the SQLIMSDA
  • SQLIMSD to indicate the number of variables used in the SQLIMSDA when processing the statement
  • SQLIMSVAR occurrences to indicate the attributes of the variables

The SQLIMSDA must have enough storage to contain all SQLIMSVAR occurrences. Each SQLIMSVAR occurrence describes a host variable or buffer into which a value in the result table is to be assigned. For more information on the SQLIMSDA, which includes a description of the SQLIMSVAR and an explanation on how to determine the number of SQLIMSVAR occurrences, see SQL descriptor area (SQLIMSDA).

SQLIMSD must be set to a value greater than or equal to zero and less than or equal to SQLIMSN.

cursor-name
Identifies the cursor to be used in the fetch operation. The cursor name must identify a declared cursor or an allocated cursor. When the FETCH statement is executed, the cursor must be in the open state.

Example

Example 1: The FETCH statement fetches the results of the SELECT statement into the application program variables HOSPCODE and HOSPNAME. When no more rows remain to be fetched, the not found condition is returned.

EXEC SQLIMS                         
  DECLARE C1 CURSOR FOR DYSQL 
END-EXEC.                        

EXEC SQLIMS                               
   PREPARE DYSQL FROM :SELECT-STATEMENT   
END-EXEC           

EXEC SQLIMS OPEN C1 END-EXEC.
 
EXEC SQLIMS FETCH C1 INTO :HOSPCODE, :HOSPNAME END-EXEC.
 
   IF SQLIMSCODE = 100
      PERFORM DATA-NOT-FOUND
   ELSE
      PERFORM GET-REST-OF-HOSP
      UNTIL SQLIMSCODE IS NOT EQUAL TO ZERO.
 
   EXEC SQLIMS CLOSE C1 END-EXEC.