August 5th, 2009

Host Programs, DECLARE, OPEN, CLOSE CURSOR (Part 3 of 3)

Posted by admin in S. Host Programs

A cursor does not point to any rows until it is opened. When you execute the OPEN statement, the underlying SELECT creates the result table to which the cursor points.
The syntax of OPEN cursor is:

          EXEC SQL
              OPEN cursor_name
          END-EXEC

So, let us open the VENDOR_CSR:

          EXEC SQL
              OPEN VENDOR_CSR
          END-EXEC

Now we can process the rows selected by the cursor.
When we are finished with the results table, we should CLOSE the cursor with the following syntax:

          EXEC SQL
              CLOSE cursor_name
          END-EXEC

So, let us close the VENDOR_CSR:

          EXEC SQL
              CLOSE VENDOR_CSR
          END-EXEC

If you are only going to use a cursor once, the end of the program execution will close the cursor. But it is good programming to CLOSE the cursor. The following COMMIT statement (which makes all changes permanent) also closes the cursors:

          EXEC SQL
              COMMIT
          END-EXEC

Comments are closed.

Sorry, the comment form is closed at this time.