Opens a previously declared dynamic cursor.
exec sql [at connection_name] open cursor_name [row_count = size] [using {host_var_list | descriptor descriptor_name | sql descriptor descriptor_name}] end-exec
Names a cursor that has been declared using the declare cursor statement.
The number of rows moved in a network roundtrip, not the number fetched into the host variable. The size argument can be either a literal or a declared host variable.
Names the host variables that contain the values for dynamic parameter markers.
Identifies descriptor_name as a SQLDA structure.
Identifies descriptor_name as a SQL descriptor.
Names the dynamic descriptor that contains information about the dynamic parameter markers in a prepared statement.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 DYNABUF PIC X(200).
01 TITLE-ID PIC X(6).
01 LNAME PIC X(15).
01 FNAME PIC X(15).
01 PHONE PIC X(15).
EXEC SQL END DECLARE SECTION END-EXEC.
...
MOVE "SELECT a.au_lname, a.au_fname, a.phone
FROM authors a, titleauthor t
WHERE a.au_id = t.au_id
AND t.title_id = ? " TO DYNABUF.
EXEC SQL PREPARE dynastmt FROM :DYNABUF END-EXEC.
EXEC SQL DECLARE who_wrote CURSOR FOR dynastmt END-EXEC.
DISPLAY "LIST AUTHORS FOR WHAT TITLE ? "
ACCEPT TITLE-ID.
EXEC SQL OPEN who_wrote USING :TITLE-ID END-EXEC.
PERFORM FETCH-LOOP UNTIL SQLCODE = 100.
EXEC SQL CLOSE who_wrote END-EXEC.
EXEC SQL DEALLOCATE CURSOR who_wrote END-EXEC.
EXEC SQL DEALLOCATE dynastmt END-EXEC.
...
FETCH-LOOP.
EXEC SQL FETCH who_wrote INTO
:LNAME, :FNAME, :PHONE END-EXEC
DISPLAY "LAST NAME : ", LNAME
DISPLAY "FIRST NAME : ", FNAME
DISPLAY "PHONE : ", PHONE.
END-FETCH-LOOP.
open executes the statement specified in the corresponding declare cursor statement. You can then use the fetch statement to retrieve the results of the prepared statement.
You can have any number of open cursors.
The using clause substitutes host-variable or dynamic-descriptor contents for the dynamic parameter markers (“?”) in the select statement.
close, declare, fetch, prepare