DB2 Sensitive Dynamic-scroll Cursor Example


Input -

employee_details table

Scenario1 - Declare the SENSITIVE DYNAMIC SCROLL CURSOR for updating column manager_id of employee_details table.

   EXEC SQL 
	DECLARE E1 SENSITIVE DYNAMIC SCROLL CURSOR FOR
     	SELECT EMP_NAME,
               MANAGER_ID
     	FROM   EMPLOYEE_DETAILS
        FOR    UPDATE OF MANAGER_ID
   END-EXEC.

Scenario2 - Declare the SENSITIVE DYNAMIC SCROLL CURSOR for updating all columns of the employee_details table.

    EXEC SQL 
	DECLARE E2 SENSITIVE DYNAMIC SCROLL CURSOR FOR
     	SELECT EMP_ID,
               EMP_NAME,
               DESIGNATION,
               MANAGER_ID,
               DATE_OF_HIRE,
               SALARY,
               DEPT_ID
     	FROM   EMPLOYEE_DETAILS 
     	FOR UPDATE 
   END-EXEC.

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

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

Scenario4 - SENSITIVE FETCH for getting emp_id, emp_name on sensitive scroll readonly cursor.

DECLARE CURSOR

    EXEC SQL 
	DECLARE E4 SENSITIVE STATIC 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 SENSITIVE E4 
     	 INTO :HV-EMP-ID,
              :HV-EMP-NAME
   END-EXEC.

Practical Example -


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

Requirement - Insert employee after the Sensitive dynamic cursor opened.

Table Before INSERT -

Table Before Insert Output

Code -

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

       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

           EXEC SQL
             INCLUDE SQLCA
           END-EXEC.

           EXEC SQL
             INCLUDE EMPLDET 
           END-EXEC.

      * DECLARING UPDATE   CURSOR 
           EXEC SQL
               DECLARE CSR_SDSCROLL SENSITIVE DYNAMIC 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_SDSCROLL
           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_SDSCROLL
           END-EXEC.
 
           STOP RUN.

       FETCH-EMP-DETAILS.

      * FETCH CURSOR
           EXEC SQL
                FETCH CSR_SDSCROLL
                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.

Program Output -

SENSITIVE DYNAMIC SCROLL CURSOR Program Output

Table After Insert -

Table After Insert Output