Examples

Example 1: Write the embedded statements in a COBOL program that will:

  1. Define a cursor C1 that is to be used to retrieve all rows from the DEPARTMENT table for departments that are administered by (ADMRDEPT) department 'A00'
  2. Place the cursor C1 before the first row to be fetched.
   EXEC SQL  DECLARE C1 CURSOR FOR
              SELECT DEPTNO, DEPTNAME, MGRNO FROM DEPARTMENT
                 WHERE ADMRDEPT = 'A00'  END-EXEC.

   EXEC SQL  OPEN C1  END-EXEC.

Example 2: Code an OPEN statement to associate a cursor DYN_CURSOR with a dynamically defined select-statement in a C program. Assume each prepared select-statement always defines two items in its select list with the first item having a data type of integer and the second item having a data type of VARCHAR(64). (The related host variable definitions, PREPARE statement, and DECLARE CURSOR statement are also shown in the example below.)

  EXEC SQL  BEGIN DECLARE SECTION;
     static short hv_int;
     char hv_vchar64[64];
     char stmt1_str[200];
  EXEC SQL  END DECLARE SECTION;

  EXEC SQL  PREPARE STMT1_NAME FROM :stmt1_str;

  EXEC SQL  DECLARE DYN_CURSOR CURSOR FOR STMT1_NAME;

  EXEC SQL  OPEN DYN_CURSOR USING :hv_int, :hv_vchar64;

Example 3: Code an OPEN statement as in example 3, but in this case the number and data types of the items in the select statement are not known.

  EXEC SQL  BEGIN DECLARE SECTION;
     char stmt1_str[200];
  EXEC SQL  END DECLARE SECTION;
  EXEC SQL  INCLUDE SQLDA;

  EXEC SQL  PREPARE STMT1_NAME FROM :stmt1_str;
  EXEC SQL  DECLARE DYN_CURSOR CURSOR FOR STMT1_NAME;

  EXEC SQL  OPEN DYN_CURSOR USING DESCRIPTOR :sqlda;