(We will difference between static and dynamic cursors in next post. Here we will see broadly what is sensitive/insensitive cursor)
To start with we will see first what is an insensitive cursor.Let us see how the keyword 'INSENSITIVE' is used to declare this type of cursor.
(We will see difference between STATIC and DYNAMIC cursor in next post)
DECLARE C1 INSENSITIVE SCROLL CURSOR FOR
SELECT STAFF_ID,NAME FROM EMP
....
OPEN C1
...
FETCH C1
..
CLOSE C1
We will see how and where the INSENSITIVE feature is shown below.
Once we open the C1 cursor, a temporary table is created in the WORK File database which holds the data from the base table ,ie, EMP table. (We must know that to use scrollable cursors we must use declared temporary tables.DB2 places the rows returned from the cursor into these Temporary tables.)
TIMELINE T1: The Cursor is opened and result set is fetched. We will see that the base table EMP and the result table(the result of cursor open statement) is same
STAGE1: The cursor is fetched and Main table and result table looks same |
STAGE2: Row deleted from main table , yet the cursor result table shows the deleted row!! |
Lets see now SENSITIVE CURSOR:
DECLARE C1 SENSITIVE STATIC SCROLL CURSOR FOR
SELECT STAFF_ID,NAME FROM EMP
....
Proceeding in the same way, let us review the Timelines T1 and T2 of the cursor life cycle.
TIMELINE T1: The Cursor is opened and result set is fetched. We will see that the base table EMP and the result table(the result of cursor open statement) is same. This is the same as shown above.
TIMELINE T2 (T2>T1) : After some time T2,user deletes one row from the main table ie the EMP table.But since the cursor is SENSITIVE, the Temporary table will sense the modification done,ie the delete and will also delete the corresponding row in the Temporary table.
In Timeline T2, the cursor fetched reflects the same change as the base table EMP. |
Lets look at the FETCH statements for these scrollable type cursors.
All the possible FETCH options are shown | here |
FETCH SENSITIVE ABSOLUTE +5 FROM C1
Fetch the FIRST ROW
FETCH SENSITIVE FIRST FROM C1
Move forward:
FETCH NEXT FROM C1
Move Backward:
FETCH PRIOR FROM C1
Fetch 5th last row:
FETCH ABSOLUTE -5 FROM C1
RELATIVE n - will FETCH the row that is n rows away from the last row fetched
ABSOLUTE n - will FETCH the row that is n rows away from the first row in the results set
.....
ReplyDelete......
...