Skip to main content
Previous sectionNext section

OPEN

Opens a cursor.

Synopsis

OPEN cursor-name

Arguments

Argument Description
cursor-name The name of the cursor, which has already been declared. The cursor name was specified in the DECLARE statement. Cursor names are case-sensitive.

Description

An OPEN statement opens a cursor according to the parameters specified in the cursor’s DECLARE statement. Once opened, a cursor can be fetched. An open cursor must be closed.

  • Attempting to open a cursor that is not declared results in an SQLCODE -52 error.

  • Attempting to open a cursor that is already open results in an SQLCODE -101 error.

  • Attempting to fetch or close a cursor that is not open results in an SQLCODE -102 error.

A successful OPEN sets SQLCODE = 0, even if the result set is empty.

As an SQL statement, this is only supported from embedded SQL. Equivalent operations are supported through ODBC using the ODBC API.

Example

The following embedded SQL example shows a cursor (named EmpCursor) being opened and closed:

   SET name="LastName,FirstName",state="##"
   &sql(DECLARE EmpCursor CURSOR FOR 
        SELECT Name, Home_State
        INTO :name,:state FROM Sample.Person
        WHERE Home_State %STARTSWITH 'A')
   WRITE !,"BEFORE: Name=",name," State=",state 
   &sql(OPEN EmpCursor)
   IF SQLCODE '= 0 { WRITE "Open error: ",SQLCODE
                     QUIT }
   NEW %ROWCOUNT,%ROWID
   FOR { &sql(FETCH EmpCursor)
        QUIT:SQLCODE  
        WRITE !,"DURING: Name=",name," State=",state }
   WRITE !,"FETCH status SQLCODE=",SQLCODE
   WRITE !,"Number of rows fetched=",%ROWCOUNT
   &sql(CLOSE EmpCursor)
   WRITE !,"AFTER: Name=",name," State=",state
Copy code to clipboard

See Also