OPEN CURSOR

Basic form 2

OPEN CURSOR [WITH HOLD] c FOR SELECT ... .

Effect

Opens a database cursor c in a database table or view for a SELECT statement. The variable c must be of the type CURSOR. You can use any SELECT statement that returns a table, but not a single record, as a result. When the cursor has been opened, the dataset specified with SELECT can be read with FETCH until the cursor is closed.

OPEN CURSOR belongs to the Open SQL command set.

If you attempt to open a cursor that has already been opened, you get a runtime error.

The following events close a cursor:

  1. The CLOSE CURSOR statement.
  2. The Open SQL statement COMMIT WORK
  3. A database commit in Native SQL. In this case, a cursor opened with WITH HOLD is not closed.
  4. The Open SQL statement ROLLBACK WORK
  5. A database rollback in Native SQL
  6. A screen change, in particular the statements CALL SCREEN, CALL DIALOG, CALL TRANSACTION, MESSAGE
  7. A Remote Function Call, in particular the statements CALL FUNCTION ... DESTINATION , CALL FUNCTION ... STARTING NEW TASK , CALL FUNCTION ... IN BACKGROUND TASK and WAIT.


Example

Open the database cursor C1 in the database table SFLIGHT for a SELECT statement

DATA: C1 TYPE CURSOR.

OPEN CURSOR C1 FOR
     SELECT * FROM SFLIGHT WHERE CARRID = 'LH '.

Notes

  1. In the above example, the OPEN statement contains no INTO clause. With cursor processing, you must always specify the target area for the selected data in the FETCH statement.


  2. The OPEN CURSOR statement allows you to open several cursors at the same time in a table. Unlike with SELECT, you thus have several independent access paths to this table.


  3. Since you can open only a restricted number of cursors at the same time, you should close cursors that are no longer required with CLOSE CURSOR.


  4. Since the OPEN statement does not support authorization checks, you must program these yourself.


Related

SELECT, FETCH and CLOSE.

Additional help

Reading Data by Cursor