Previous Topic: Cursor UsageNext Topic: Example 2


Example1

In this example, the DECLARE CURSOR statement associates the cursor name C1 with the results of the SELECT. This example also shows how the cursor is opened with the OPEN statement, used in a FETCH statement, and closed with a CLOSE statement.

     EXEC SQL
         DECLARE C1 CURSOR FOR
             SELECT DEPTNO, DEPTNAME, MGRNO
             FROM DEPTTBL
             WHERE ADMDEPT = 'A0'
     END-EXEC.
 ...
    * DISPLAY DEPT TABLE INFO
     EXEC SQL
         WHENEVER NOT FOUND   GO TO DEPT-FETCH-LP-END
         WHENEVER SQLERROR    GO TO SQL-ERROR-RTN
         WHENEVER SQLWARNING  CONTINUE
     END-EXEC.
     EXEC SQL
         OPEN C1
     END-EXEC.
 DEPT-FETCH-LP.
     EXEC SQL
         FETCH C1 INTO :DNUM, :DNAME, :MNUM
     END-EXEC.
     DISPLAY DNUM, DNAME, MNUM.
     GO TO FETCH-LOOP.
 DEPT-FETCH-LP-END.
     EXEC SQL
         CLOSE C1
     END-EXEC.
 ...