• Tidak ada hasil yang ditemukan

Holes in the result table of a scrollable cursor

Dalam dokumen Application Programming and SQL Guide (Halaman 182-185)

Insomesituations,youmightnotbe abletofetcha rowfromtheresulttableofa scrollablecursor,dependingonhow thecursorisdeclared:

v Scrollablecursorsthatare declaredasINSENSITIVEorSENSITIVESTATIC followa staticmodel, whichmeansthatDB2determinesthesizeoftheresult tableandtheorderoftherowswhenyouopenthecursor.

Deletingorupdatingrowsaftera staticcursorisopencanresultinholesinthe resulttable,whichmeansthattheresult tabledoesnotshrinktofillthespaceof deletedrowsorthespaceofrowsthathavebeen updatedand nolongersatisfy thesearchcondition.Youcannotaccessadeletehole oranupdateholeofa staticcursor,althoughyoucanremoveholesinspecific situations;see

“Removingadeleteholeoran updatehole”onpage146.

v

Scrollablecursorsthatare declaredasSENSITIVEDYNAMICfollowadynamic model,whichmeansthatthesizeandcontentsoftheresulttable,andtheorder oftherows,canchangeafteryouopenthecursor.

Adynamiccursorscrollsdirectlyonthebase table.Ifthecurrentrowof the cursorisdeletedorif itisupdatedsothatitnolongersatisfiesthesearch condition,andthenextcursoroperationisFETCHCURRENT,thenDB2 issues anSQLwarning.

The followingexamples demonstratehowdeleteandupdateholescanoccurwhen youusea SENSITIVESTATIC scrollablecursor.

Creatinga deleteholewithastaticscrollable cursor: SupposethattableA consistsofoneintegercolumn,COL1,whichhasthevaluesshown inFigure16.

Now supposethatyoudeclarethefollowingSENSITIVESTATICscrollablecursor,

whichyouusetodeleterowsfromA:

Figure16.ValuesforCOL1oftableA

EXEC SQL DECLARE C3 SENSITIVE STATIC SCROLL CURSOR FOR

SELECT COL1

FROM A

FOR UPDATE OF COL1;

Now youexecutethefollowingSQLstatements:

EXEC SQL OPEN C3;

EXEC SQL FETCH ABSOLUTE +3 C3 INTO :HVCOL1;

EXEC SQL DELETE FROM A WHERE CURRENT OF C3;

Thepositioned deletestatementcreates adeletehole,asshowninFigure17.

Afteryouexecutethepositioneddeletestatement, thethirdrowisdeletedfrom theresult table,but theresult tabledoesnotshrink tofillthespacethatthedeleted row creates.

Creatinganupdateholewithastaticscrollable cursor: Supposethatyoudeclare thefollowingSENSITIVESTATICscrollablecursor,whichyouusetoupdaterows inA:

EXEC SQL DECLARE C4 SENSITIVE STATIC SCROLL CURSOR FOR

SELECT COL1

FROM A

WHERE COL1<6;

Now youexecutethefollowingSQLstatements:

EXEC SQL OPEN C4;

UPDATE A SET COL1=COL1+1;

Thesearched UPDATEstatementcreatesanupdatehole,asshowninFigure18on page146.

Figure17.Creatingadeletehole

AfteryouexecutethesearchedUPDATEstatement, thelastrownolongerqualifies for theresult table,buttheresult tabledoesnotshrinktofillthespacethatthe disqualified rowcreates.

Removinga deleteholeoranupdatehole: Youcanremoveadeleteholeor an updatehole inspecificsituations.

Ifyoutrytofetchfroma deletehole,DB2issuesanSQLwarning.Ifyoutryto updateordeletea deletehole, DB2issuesan SQLerror.Youcanremovea delete hole onlybyopeningthescrollablecursor,settinga savepoint,executinga positioned DELETEstatementwiththescrollablecursor,and rollingbacktothe savepoint.

Ifyoutrytofetchfroman updatehole,DB2 issuesanSQLwarning.Ifyoutryto deletean updatehole,DB2 issuesanSQLerror. However,youcanconvertan updatehole backtoa resulttablerow byupdatingtherowinthebasetable,as shown inFigure19onpage147.Youcanupdatethebasetable witha searched UPDATE statementinthesameapplication process,orasearched orpositioned UPDATE statementinanotherapplication process.Afteryouupdatethebasetable, if therowqualifiesfortheresulttable,theupdateholedisappears.

Figure18.Creatinganupdatehole

Ahole becomesvisibletoacursorwhena cursoroperationreturnsa non-zero SQLCODE.Thepointat whicha holebecomesvisible dependsonthefollowing factors:

v Whetherthescrollablecursorcreatesthehole

v WhethertheFETCHstatementisFETCHSENSITIVEorFETCHINSENSITIVE Ifthescrollablecursorcreatesthehole,theholeisvisiblewhenyouexecutea FETCHstatementfortherowthatcontainsthehole.TheFETCHstatementcanbe FETCHINSENSITIVEorFETCHSENSITIVE.

Ifan updateordeleteoperationoutsidethescrollablecursorcreatesthehole,the hole isvisibleatthefollowingtimes:

v IfyouexecuteaFETCHSENSITIVEstatementfortherowthatcontains thehole, thehole isvisible whenyouexecutetheFETCHstatement.

v IfyouexecuteaFETCHINSENSITIVEstatement,thehole isnotvisiblewhen youexecutetheFETCHstatement. DB2returnstherow asit wasbeforethe updateordeleteoperationoccurred.However, ifyoufollowtheFETCH INSENSITIVEstatementwith apositionedUPDATE orDELETEstatement,the holebecomesvisible.

Dalam dokumen Application Programming and SQL Guide (Halaman 182-185)