Examples: Using a cursor
These examples show the SQL statements that you can include in a program to define and work with a serial and a scrollable cursor.
Suppose that your program examines data about people in department D11. You can use either a serial or a scrollable cursor to obtain information about the department from the CORPDATA.EMPLOYEE table.
For the serial cursor example, the program processes all of the rows from the table, updating the job for all members of department D11 and deleting the records of employees from the other departments.
| Serial cursor SQL statement | Described in section |
|---|---|
| EXEC SQL |
Step 1: Defining the cursor. |
| EXEC SQL |
Step 2: Opening the cursor. |
| EXEC SQL |
Step 3: Specifying what to do when the end of data is reached. |
| EXEC SQL |
Step 4: Retrieving a row using a cursor. |
| ... for all employees |
Step 5a: Updating the current row. |
| ... for other employees, |
Step 5b: Deleting the current row. |
| Branch back to fetch and process the next row. | |
| CLOSE-THISEMP. |
Step 6: Closing the cursor. |
For the scrollable cursor example, the program uses the RELATIVE position option to obtain a representative sample of salaries from department D11.
| Scrollable cursor SQL statement | Described in section |
|---|---|
| EXEC SQL |
Step 1: Defining the cursor. |
| EXEC SQL |
Step 2: Opening the cursor. |
| EXEC SQL |
Step 3: Specifying what to do when the end of data is reached. |
| ...initialize program summation |
Step 4: Retrieving a row using a cursor. |
| ...calculate the average |
|
| CLOSE-THISEMP. |
Step 6: Closing the cursor. |