Step 6: Closing the cursor

If you have processed the rows of a result table using a serial cursor, and you want to use the cursor again, issue a CLOSE statement to close the cursor before opening it again.

The statement looks like this:

EXEC SQL
 CLOSE cursor-name
END-EXEC.

If you processed the rows of a result table and you do not want to use the cursor again, you can let the system close the cursor. The system automatically closes the cursor when:

  • A COMMIT without HOLD statement is issued and the cursor is not declared using the WITH HOLD clause.
  • A ROLLBACK without HOLD statement is issued.
  • The job ends.
  • The activation group ends and CLOSQLCSR(*ENDACTGRP) was specified on the precompile.
  • The first SQL program in the call stack ends and neither CLOSQLCSR(*ENDJOB) or CLOSQLCSR(*ENDACTGRP) was specified when the program was precompiled.
  • The connection to the application server is ended using the DISCONNECT statement.
  • The connection to the application server was released and a successful COMMIT occurred.
  • An *RUW CONNECT occurred.

    Because an open cursor still holds locks on referred-to-tables or views, you should explicitly close any open cursors as soon as they are no longer needed.