DB2 Non Scrollable Cursor Example


Input -

employee_details table

Scenario1 - Declare non-scrolling cursor for retrieving all employee names from employee_details table.

   EXEC SQL 
	DECLARE E1 NO SCROLL CURSOR FOR
     	SELECT EMP_NAME
     	FROM   EMPLOYEE_DETAILS 
   END-EXEC.

Scenario2 - Declare the cursor for retrieving all employee names from the employee_details table.

   EXEC SQL 
	DECLARE E2 CURSOR FOR
     	SELECT EMP_NAME
     	FROM   EMPLOYEE_DETAILS 
   END-EXEC.

Scenario3 - Declare the cursor for retrieving employee names under department 1000 from the employee_details table.

   EXEC SQL 
	DECLARE E3 NO SCROLL CURSOR FOR
     	SELECT EMP_NAME
     	FROM   EMPLOYEE_DETAILS 
		WHERE  DEPT_ID = 1000 
   END-EXEC.

Scenario4 - Declare non-scrolling cursor for retrieving all employee names from highest salary to lowest.

   EXEC SQL 
	DECLARE E4 CURSOR FOR
     	SELECT EMP_NAME
     	FROM   EMPLOYEE_DETAILS 
		ORDER  BY SALARY DESC 
   END-EXEC.

Scenario5 - Declare a non-scrolling cursor for retrieving a sum of salaries at the designation level.

   EXEC SQL 
	DECLARE E2 CURSOR FOR
     	SELECT DESIGNATION,
		       SUM(SALARY) SALARIES_TOTAL
     	FROM   EMPLOYEE_DETAILS 
		GROUP  BY DESIGNATION
   END-EXEC.

Scenario6 - Declare non-scrolling cursor for retrieving a sum of salaries > 18000 at designation level.

   EXEC SQL 
	DECLARE E2 CURSOR FOR
     	SELECT DESIGNATION,
		       SUM(SALARY) SALARIES_TOTAL
     	FROM   EMPLOYEE_DETAILS 
		GROUP  BY DESIGNATION
		HAVING SUM(SALARY) > 18000
   END-EXEC.

Scenario7 - Declare non-scrolling cursor for retrieving employee names under department 1000 from employee_details table and hike 2000 salary.

   EXEC SQL 
	DECLARE E2 CURSOR FOR
     	SELECT EMP_NAME,
		       SALARY
     	FROM   EMPLOYEE_DETAILS
        WHERE  DEPT_ID = 1000
        FOR    UPDATE OF SALARY		
   END-EXEC.

Practical Example -


Scenario - The below example describes how the non-scrollable cursor is coded in the COBOL + DB2 program.

Requirement - Display employee_details table data for department 1000.

NON SCROLLABLE CURSOR Example Code -

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

       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

           EXEC SQL
             INCLUDE SQLCA
           END-EXEC.

           EXEC SQL
             INCLUDE EMPLDET 
           END-EXEC.

      * DECLARING READONLY CURSOR
           EXEC SQL
                DECLARE CSR-NSCROLL NO SCROLL CURSOR FOR 
                 SELECT EMP_ID,
                        EMP_NAME,
                        DESIGNATION,
                        MANAGER_ID,
                        DATE_OF_HIRE,
                        SALARY,
                        DEPT_ID
                   FROM EMPLOYEE_DETAILS
                  WHERE DEPT_ID = 1000
           END-EXEC.
 
       PROCEDURE DIVISION.

      * OPENING CURSOR 
           EXEC SQL
                OPEN CSR-NSCROLL
           END-EXEC.
 
      * 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-NSCROLL
           END-EXEC.

           STOP RUN.

       FETCH-EMP-DETAILS.

      * FETCH CURSOR
           EXEC SQL
                FETCH CSR-NSCROLL
                INTO   :EMP-ID,
                       :EMP-NAME,
                       :DESIGNATION,
                       :MANAGER-ID
                       :MANAGER-ID-NI,
                       :DATE-OF-HIRE,
                       :SALARY,
                       :DEPT-ID
           END-EXEC.

           EVALUATE SQLCODE
               WHEN ZERO
                    DISPLAY  EMP-ID, "|", EMP-NAME, "|",
                             DESIGNATION, "|", MANAGER-ID, "|",
                             DATE-OF-HIRE, "|", SALARY, "|", DEPT-ID    
               WHEN 100
                    CONTINUE
               WHEN OTHER
                    DISPLAY "DB2 ERROR:  ", SQLCODE 
           END-EVALUATE. 
  
       FETCH-EMP-DETAILS-EXIT.                                          
           EXIT.

Output -

NON SCROLLABLE CURSOR Program Output