By default, cursors are moved by 1 row at a time in the result table in sequential order starting from the first row. To remove this limitation SCROLL clause is used to indicate whether the cursor moves sequentially through the result table or can move randomly through the result table. You can declare row-positioned or rowset-positioned cursors in a number of ways. These cursors can be scrollable or not scrollable, held or not held, or returnable or not returnable.
►►─ DECLARE cursor-name ─┬─ NO SCROLL ◄ ──────────────────────────┬─ CURSOR ──► ├─ ASENSITIVE ◄ ──────────────┬─ SCROLL ─┘ ├─ INSENSITIVE ───────────────┤ └─ SENSITIVE ─┬─ DYNAMIC ◄ ─┬─┘ └─ STATIC ────┘ ►──┬─────────────┬─ FOR ─┬─ select-statement ─┬──────────────────────────►◄ └─ WITH HOLD ─┘ └─ statement-name ───┘
A non-scrollable cursor can be either row-positioned or rowset-positioned.
Note: Rows are sensitive to changes if a temporary table is not used. If scrolling is not specified, the default is NO SCROLL.
Processing: A non-scrollable cursor always moves sequentially forward in the result table.
A scrollable cursor can be either row-positioned or rowset-positioned. With scrollable cursors, you can move directly to the rows you want without having to FETCH every other row returned by the cursor.
If you want to order the rows of the cursor’s result set, and you also want the cursor to be updatable, you need to declare the cursor as scrollable, even if you use it only to retrieve rows (or rowsets) sequentially.
EXEC SQL DECLARE CURSOR-01 INSENSITIVE SCROLL CURSOR FOR SELECT EMPNO, DEPTNO FROM PROD.EMPDEPT ORDER BY EMPNO END-EXEC.
The size, the order of the rows, and the values for each row of the result table do not change after the cursor opens.
This is the read Only cursor where the positioned updates/deletes are not allowed and even the updates by other applications to the table do not affect the result table. Rows that are inserted into the underlying table are not added to the result table & rows in the result table remain the same with the same values.
EXEC SQL DECLARE CURSOR-02 SENSITIVE STATIC SCROLL CURSOR FOR SELECT EMPNO, DEPTNO FROM PROD.EMPDEPT ORDER BY EMPNO END-EXEC.
The size of the result table, order of the rows remains the same after the cursor opens i.e.
EXEC SQL DECLARE CURSOR-02 SENSITIVE DYNAMIC SCROLL CURSOR FOR SELECT EMPNO, DEPTNO FROM PROD.EMPDEPT ORDER BY EMPNO END-EXEC.
The size and contents of the result table can change with every fetch e.g. another program changes & commits the data.
The order of the rows can change after the cursor is opened e.g. columns that are in the ORDER BY clause are updated after the cursor is opened, the order of the rows in the result table changes.
Positioned UPDATE and DELETE statement changes are visible. Also committed insert, update, or delete operations outside the cursor but impacting the result table are visible.
Note: As the FETCH statement executes against the base table, the cursor needs no temporary result table.
EXEC SQL DECLARE CURSOR-02 ASENSITIVE SCROLL CURSOR FOR SELECT EMPNO, DEPTNO FROM PROD.EMPDEPT ORDER BY EMPNO END-EXEC.
Specifies that the cursor is SENSITIVE DYNAMIC if a temporary table is not used. Otherwise, it is INSENSITIVE. This is the same as NO SCROLL except that scroll options are available with the FETCH statement. It will never be a STATIC cursor
►►─ FETCH ─┬───────────────┬──┤ fetch-orientation ├──┬──────────┬─ cursor-name ─► ├─ INSENSITIVE ─┤ └─ FROM ◄ ─┘ └─ SENSITIVE ───┘ ►──┬──────────────────────┬─────┬───────────────────────────────────┬──────────►◄ └─┤ single-row-fetch ├─┘ ├ ┌─ , ─────────────┐ ┤ ├─ INTO ─▼─ host-variable ─┴────────┤ └─ USING DESCRIPTORdescriptor-name ─┘ Expansion of fetch-orientation ├──┬── BEFORE ──────────┬──────────────────────────────────────┤ ├── AFTER ───────────┤ └─┤ row-positioned ├─┘ Expansion of row-positioned ├──┬── NEXT ────────────────────────────┬───────────────────────┤ ├── PRIOR ───────────────────────────┤ ├── FIRST ───────────────────────────┤ ├── LAST ────────────────────────────┤ ├── ABSOLUTE──┬─ host-variable ────┬─┤ ├ └─ integer-constant ─┘ ┤ └── RELATIVE ─┬─ host-variable ────┬─┘ └─ integer-constant ─┘ Expansion of single-row-fetch ├──┬───────────────────────────────────┬─────────────────────────┤ ├ ┌─ , ─────────────┐ ┤ ├─ INTO ─▼─ host-variable ─┴────────┤ └─ INTO DESCRIPTOR descriptor-name ─┘
The following keywords are supported when fetching data from a scrollable cursor:
ABSOLUTE: The cursor is positioned to the row specified from the beginning, or end if negative, of the result set. If the position is zero, the cursor is positioned before the first row of the set. If the position value is greater than the number of rows in the result set, a warning is issued and the position is changed to after the last row.
Examples with a set of 100 rows:
FETCH ABSOLUTE 100 EMPNO, DEPTNO INTO :EMPNO, :DEPTNO
RELATIVE: The cursor is positioned from the current position. Negative value positions backward, and a positive value forwards. Zero returns the current row; however, if the current row has been deleted or updated such that it no longer qualifies, then the next row of the set is returned for the dynamic sensitive cursor mode.
FETCH RELATIVE 100 EMPNO, DEPTNO INTO :EMPNO, :DEPTNO
A well-maintained product backlog is crucial for successful product development. It serves as a single…
Incremental value to the customer refers to the gradual delivery of small, functional parts of…
A Product Market refers to the group of potential customers who might be interested in…
The Professional Agile Leadership - Evidence-Based Management (PAL-EBM) certification offered by Scrum.org is designed for…
The Professional Agile Leadership (PAL I) certification, offered by Scrum.org, is designed to equip leaders…
Choosing the right Scrum Master Certification depends on your current experience and career goals. If…