DB2 Insensitive Scroll Cursor Example


Input -

employee_details table

Scenario1 - Declare the INSENSITIVE SCROLL CURSOR for reading employee_details table.

  EXEC SQL 
	DECLARE E1 INSENSITIVE SCROLL CURSOR FOR
     	SELECT EMP_NAME,
               MANAGER_ID
     	FROM   EMPLOYEE_DETAILS
        FOR    READ ONLY
   END-EXEC.

Scenario2 - Declare the insensitive scroll readonly cursor for designation "FRESHER" in department 1000 of employee_details table.

  EXEC SQL 
	DECLARE E2 INSENSITIVE SCROLL CURSOR FOR
     	SELECT EMP_ID,
               EMP_NAME,
               DESIGNATION
     	FROM   EMPLOYEE_DETAILS 
     	WHERE  DESIGNATION = "FRESHER"
		AND    DEPT_ID = 1000
     	FOR    READ ONLY 
   END-EXEC.

Scenario3 - INSENSITIVE FETCH for getting emp_id, emp_name on insensitive scroll readonly cursor.

DECLARE CURSOR

  EXEC SQL 
	DECLARE E3 INSENSITIVE SCROLL CURSOR FOR
     	SELECT EMP_ID,
               EMP_NAME
     	FROM   EMPLOYEE_DETAILS 
     	WHERE  DESIGNATION = "FRESHER"
		AND    DEPT_ID IN (1000, 2000)
		ORDER  BY DEPT_ID ASC
     	FOR    READ ONLY
   END-EXEC.

FETCH -

  EXEC SQL 
	    FETCH INSENSITIVE E3
     	 INTO :HV-EMP-ID,
              :HV-EMP-NAME
   END-EXEC.

Practical Example -


Scenario - The below example describes how the INSENSITIVE SCROLL CURSOR is coded in the COBOL + DB2 program.

Requirement - Insert employee after the insensitive cursor opened.

Table Before Update -

Table Before Insert Output

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. CISENSIT.

       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
   
           EXEC SQL
             INCLUDE SQLCA 
           END-EXEC.

           EXEC SQL
             INCLUDE EMPLDET
           END-EXEC.

      * DECLARING READ ONLY CURSOR
           EXEC SQL
               DECLARE CSR_INSCROLL INENSITIVE SCROLL CURSOR FOR
               SELECT EMP_ID,
                      EMP_NAME,
                      DESIGNATION,
                      MANAGER_ID,
                      SALARY
                FROM  EMPLOYEE_DETAILS
               WHERE  DESIGNATION = 'TESTER' 
                 AND  DEPT_ID = 2000
                 FOR  READ ONLY 
           END-EXEC. 

       PROCEDURE DIVISION.

      * OPENING CURSOR
           EXEC SQL
                OPEN CSR_SSSCROLL
           END-EXEC.

      * INSERTING A EMPLOYEE THAT MATCHES TO THE CURSOR CONDITION
           PERFORM INSERT-EMP-DETAILS 
              THRU INSERT-EMP-DETAILS-EXIT. 
 
      * FETCHING CURSOR LOOP UNTIL END OF RESULT TABLE  
           IF SQLCODE EQUAL ZERO 
              PERFORM FETCH-EMP-DETAILS 
                 THRU FETCH-EMP-DETAILS-EXIT
                UNTIL SQLCODE NOT EQUAL 0
           END-IF.

      * CLOSING CURSOR
           EXEC SQL
                CLOSE CSR_SSSCROLL
           END-EXEC.

           STOP RUN.

       FETCH-EMP-DETAILS.

      * FETCH CURSOR 
           EXEC SQL
                FETCH CSR_SSSCROLL 
                INTO   :EMP-ID,
                       :EMP-NAME,
                       :DESIGNATION,
                       :MANAGER-ID
                       :MANAGER-ID-NI,
                       :SALARY
           END-EXEC.

           EVALUATE SQLCODE
               WHEN ZERO
                    DISPLAY EMP-ID " : " EMP-NAME " : " DESIGNATION
      -             " : " MANAGER-ID " : " SALARY 
               WHEN 100
                    CONTINUE 
               WHEN OTHER  
                    DISPLAY "DB2 ERROR:  ", SQLCODE 
           END-EVALUATE.

       FETCH-EMP-DETAILS-EXIT.
           EXIT.

       INSERT-EMP-DETAILS.

      * INSERT EMPLOYEE WITH EMP_ID 10
           EXEC SQL
                INSERT INTO EMPLOYEE_DETAILS
                VALUES (10, 'EMPLOYEE10', 'TESTER', 'EMPLOYEE4',
                      '2019-10-08', 15000, 2000)
           END-EXEC.

           EVALUATE SQLCODE
               WHEN ZERO
                    DISPLAY "ROW INSERTED SUCCESSFULLY"
               WHEN OTHER
                    DISPLAY "DB2 INSERT ERROR:  ", SQLCODE
           END-EVALUATE.

       INSERT-EMP-DETAILS-EXIT.
           EXIT.

Output -

INSENSITIVE SCROLL CURSOR Program Output

Table After Update -

Table After Insert Output