• Tidak ada hasil yang ditemukan

Using a scrollable cursor

Dalam dokumen Application Programming and SQL Guide (Halaman 176-181)

Tomake acursorscrollable,youdeclareitasscrollable.Ascrollablecursorcanbe either row-positionedorrowset-positioned.Tousea scrollablecursor,youexecute FETCHstatementsthatindicatewhereyouwanttopositionthecursor.For examples ofFETCHstatementsthatpositionacursorforbothrowsandrowsets, seeTable29onpage142.

Ifyouwanttoordertherowsofthecursor’sresult set,andyoualsowantthe cursortobe updatable,youneedtodeclarethecursorasscrollable,evenif you useitonlytoretrieverows(orrowsets)sequentially.YoucanusetheORDER BY clausein thedeclaration ofanupdatablecursoronlyif youdeclarethecursoras scrollable.

Declaring ascrollable cursor: Toindicatethatacursorisscrollable,youdeclareit with theSCROLLkeyword.Thefollowingexamples showacharacteristic of scrollablecursors:thesensitivity.

|

Figure13showsadeclaration foraninsensitivescrollablecursor.

Declaringascrollablecursorwith theINSENSITIVEkeyword hasthefollowing effects:

v

Thesize,theorder oftherows, andthevaluesforeachrowoftheresulttable donotchangeaftertheapplicationopens thecursor.

v Theresulttableisread-only.Therefore,youcannotdeclarethecursorwiththe FORUPDATEclause,andyoucannotusethecursorforpositioned updateor deleteoperations.

Figure14showsadeclaration forasensitivestaticscrollablecursor.

DeclaringacursorasSENSITIVESTATIChasthefollowingeffects:

v

Whentheapplicationexecutespositioned UPDATEandDELETEstatements withthecursor,those changesarevisible intheresult table.

v Whenthecurrentvalueofa rownolongersatisfiestheSELECTstatementthat wasusedinthecursordeclaration,thatrow isnolongervisibleintheresult table.

v Whena rowoftheresulttableisdeletedfromtheunderlyingtable,thatrowis nolongervisibleintheresult table.

v Changesthataremadetotheunderlyingtablebyothercursorsorother applicationprocessescanbevisibleintheresult table,dependingonwhether theFETCHstatementsthatyouusewiththecursorare FETCHINSENSITIVEor FETCHSENSITIVEstatements.

Figure15showsadeclaration forasensitivedynamicscrollablecursor.

DeclaringacursorasSENSITIVEDYNAMIChasthefollowingeffects:

v Whentheapplicationexecutespositioned UPDATEandDELETEstatements withthecursor,those changesarevisible.Inaddition,whentheapplication executesinsert,update,ordeleteoperations(within theapplicationbutoutside thecursor),thosechanges arevisible.

v

Allcommittedinserts,updates,anddeletesbyotherapplicationprocessesare visible.

EXEC SQL DECLARE C1 INSENSITIVE SCROLL CURSOR FOR

SELECT DEPTNO, DEPTNAME, MGRNO

FROM DSN8910.DEPT

ORDER BY DEPTNO END-EXEC.

Figure13.Declarationforaninsensitivescrollablerowcursor

EXEC SQL DECLARE C2 SENSITIVE STATIC SCROLL CURSOR FOR

SELECT DEPTNO, DEPTNAME, MGRNO

FROM DSN8910.DEPT

ORDER BY DEPTNO END-EXEC.

Figure14.Declarationforasensitivestaticscrollablerowcursor

EXEC SQL DECLARE C2 SENSITIVE DYNAMIC SCROLL CURSOR FOR

SELECT DEPTNO, DEPTNAME, MGRNO

FROM DSN8910.DEPT

ORDER BY DEPTNO END-EXEC.

Figure15.Declarationforasensitivedynamicscrollablecursor

v BecausetheFETCHstatementexecutesagainstthebasetable,thecursorneeds notemporaryresulttable.Whenyoudefinea cursorasSENSITIVEDYNAMIC, youcannotspecifytheINSENSITIVEkeyword inaFETCHstatementforthat cursor.

v IfyouspecifyanORDER BYclausefor aSENSITIVEDYNAMIC cursor,DB2 mightchoosean indexaccesspathif theORDER BYisfullysatisfiedbyan existingindex.However, adynamicscrollablecursorthatisdeclaredwith an ORDERBYclauseisnotupdatable.

Static scrollablecursor: BoththeINSENSITIVEcursorand theSENSITIVE STATIC cursorfollowthestaticcursormodel:

v Thesizeoftheresulttabledoesnotgrowaftertheapplicationopens thecursor.

Rowsthatareinsertedintotheunderlyingtable arenotaddedtotheresult table.

v Theorderoftherowsdoesnotchangeaftertheapplicationopens thecursor.

Ifthecursordeclaration containsanORDER BYclause,and thecolumns thatare intheORDER BYclauseareupdatedafterthecursorisopened,theorderofthe rowsintheresulttable doesnotchange.

Dynamic scrollablecursor: WhenyoudeclareacursorasSENSITIVE,youcan declareiteitherSTATIC orDYNAMIC.TheSENSITIVEDYNAMICcursorfollows thedynamiccursormodel:

v Thesizeandcontentsoftheresulttablecanchangewith everyfetch.

Thebasetablecanchangewhilethecursorisscrollingonit.Ifanother applicationprocesschanges thedata,thecursorseesthenewlychangeddata whenitiscommitted.Iftheapplicationprocessof thecursorchanges thedata, thecursorseesthenewlychangeddataimmediately.

v Theorderoftherowscanchangeaftertheapplication opensthecursor.

Ifthecursordeclaration containsanORDER BYclause,and columnsthatarein theORDER BYclauseareupdatedafterthecursorisopened,theorderofthe rowsintheresulttable changes.

Determining attributesofa cursorby checkingtheSQLCA: Afteryouopena cursor,youcandeterminethefollowingattributesofthecursorbycheckingthe followingSQLWARN andSQLERRDfields oftheSQLCA:

SQLWARN1

Indicateswhetherthecursorisscrollableornon-scrollable.

SQLWARN4

Indicateswhetherthecursorisinsensitive(I),sensitivestatic(S),orsensitive dynamic(D).

SQLWARN5

Indicateswhetherthecursorisread-only,readableand deletable,orreadable, deletable,andupdatable.

SQLERRD(1) andSQLERRD(2)

Thesetwofields togethercontainadouble byteintegerthatrepresentsthe number ofrowsin theresult tableofacursorwhenthecursorispositioned after thelastrow.Thecursorispositionedafter thelastrow whenthe SQLCODEis100.Thesefieldsarenotsetfordynamicscrollablecursors.

SQLERRD(3)

Thenumber ofrowsintheresult tablewhentheSELECTstatementof the cursorcontains adatachangestatement.

|

|

|

|

|

|

IftheOPEN statementexecuteswith noerrorsorwarnings,DB2doesnotset SQLWARN0whenit setsSQLWARN1,SQLWARN4,orSQLWARN5.

For moreinformationabouttheSQLCAfields,seethetopic“Descriptionof SQLCAfields”inDB2 SQLReference.

Determining attributesofa cursorby usingtheGETDIAGNOSTICS

statement: Afteryouopena cursor,youcandeterminethefollowingattributesof thecursorbycheckingtheseGETDIAGNOSTICSitems:

DB2_SQL_ATTR_CURSOR_HOLD

Indicateswhetherthecursorcanbeheld openacrosscommits(YorN) DB2_SQL_ATTR_CURSOR_ROWSET

Indicateswhetherthecursorcanuserowsetpositioning(YorN) DB2_SQL_ATTR_CURSOR_SCROLLABLE

Indicateswhetherthecursorisscrollable(YorN) DB2_SQL_ATTR_CURSOR_SENSITIVITY

Indicateswhetherthecursorisinsensitiveorsensitivetochanges thatare madebyotherprocesses(IorS)

DB2_SQL_ATTR_CURSOR_TYPE

Indicateswhetherthecursorisforward(F) declaredstatic(SforINSENSITIVE orSENSITIVESTATIC)ordynamic(DforSENSITIVEDYNAMIC)

For moreinformationabouttheGETDIAGNOSTICS statement,see“TheGET DIAGNOSTICS statement”onpage116.

Retrieving rowswithascrollable cursor: Whenyouopenanycursor,thecursor ispositionedbefore thefirstrowoftheresult table.Youmoveascrollablecursor around intheresult tablebyspecifyingafetchorientationkeywordina FETCH statement.Afetch orientationkeyword indicatestheabsoluteorrelativepositionof thecursorwhentheFETCHstatementisexecuted.Table28lists thefetch

orientationkeywordsthatyoucanspecifyandtheirmeanings.Thesekeywords apply tobothrow-positionedscrollablecursorsand rowset-positionedscrollable cursors.

Table28.Positionsforascrollablecursor

KeywordinFETCHstatement CursorpositionwhenFETCHisexecuted1

BEFORE Beforethefirstrow

FIRSTorABSOLUTE+1 Onthefirstrow

LASTorABSOLUTE−1 Onthelastrow

AFTER Afterthelastrow

ABSOLUTE2 Onanabsoluterownumber,frombeforethefirst

rowforwardorfromafterthelastrowbackward RELATIVE2 Ontherowthatisforwardorbackwardarelative

numberofrowsfromthecurrentrow

CURRENT Onthecurrentrow

PRIORorRELATIVE−1 Onthepreviousrow

NEXT Onthenextrow(default)

Table28.Positionsforascrollablecursor (continued)

KeywordinFETCHstatement CursorpositionwhenFETCHisexecuted1

Notes:

1. Thecursorpositionappliestobothrowpositionandrowsetposition,forexample,before thefirstroworbeforethefirstrowset.

2. FormoreinformationaboutABSOLUTEandRELATIVE,seethetopic“FETCH”inDB2 SQLReference.

Example:TousethecursorthatisdeclaredinFigure13onpage139 tofetchthe fifthrow oftheresulttable,usea FETCHstatementlikethis:

EXEC SQL FETCH ABSOLUTE +5 C1 INTO :HVDEPTNO, :DEPTNAME, :MGRNO;

Tofetch thefifthrow fromtheendof theresult table,usethisFETCHstatement:

EXEC SQL FETCH ABSOLUTE -5 C1 INTO :HVDEPTNO, :DEPTNAME, :MGRNO;

Determining thenumberofrows intheresulttablefora staticscrollable cursor: Youcandeterminehow manyrowsareintheresult tableofan INSENSITIVEor SENSITIVESTATICscrollablecursor.Todothat,executea FETCHstatement, suchasFETCHAFTER,thatpositions thecursorafter thelast row.Youcanthen examinethefields SQLERRD(1)andSQLERRD(2)intheSQLCA (fields sqlerrd[0]and sqlerrd[1]forCandC++)forthenumber ofrowsinthe result table.Alternatively,youcanusetheGETDIAGNOSTICS statementto retrievethenumberofrowsintheROW_COUNTstatementitem.

FETCHstatement interactionbetweenrowandrowsetpositioning: Whenyou declarea cursorwiththeWITHROWSETPOSITIONINGclause,youcanintermix row-positionedFETCHstatementswithrowset-positionedFETCHstatements.For informationaboutusingamultiple-rowFETCHstatement,see“Usinga

multiple-rowFETCHstatementwith hostvariablearrays”onpage131.

Table29showstheinteractionbetweenrowand rowsetpositioning forascrollable cursor.Assumethatyoudeclarethescrollablecursoronatablewith 15rows.

Table29.Interactionbetweenrowandrowsetpositioningforascrollablecursor KeywordsinFETCHstatement CursorpositionwhenFETCHisexecuted

FIRST Onrow1

FIRSTROWSET Onarowsetofsize1,consistingofrow1

FIRSTROWSETFOR5ROWS Onarowsetofsize5,consistingofrows1,2,3,4, and5

CURRENTROWSET Onarowsetofsize5,consistingofrows1,2,3,4, and5

CURRENT Onrow1

NEXT(default) Onrow2

NEXTROWSET Onarowsetofsize1,consistingofrow3

NEXTROWSETFOR3ROWS Onarowsetofsize3,consistingofrows4,5,and 6

NEXTROWSET Onarowsetofsize3,consistingofrows7,8,and 9

LAST Onrow15

Table29.Interactionbetweenrowandrowsetpositioningforascrollablecursor (continued) KeywordsinFETCHstatement CursorpositionwhenFETCHisexecuted LASTROWSETFOR2ROWS Onarowsetofsize2,consistingofrows14and

15

PRIORROWSET Onarowsetofsize2,consistingofrows12and 13

ABSOLUTE2 Onrow2

ROWSETSTARTINGAT®ABSOLUTE2 FOR3ROWS

Onarowsetofsize3,consistingofrows2,3,and 4

RELATIVE2 Onrow4

ROWSETSTARTINGATABSOLUTE2 FOR4ROWS

Onarowsetofsize4,consistingofrows2,3,4, and5

RELATIVE-1 Onrow1

ROWSETSTARTINGATABSOLUTE3 FOR2ROWS

Onarowsetofsize2,consistingofrows3and4

ROWSETSTARTINGATRELATIVE4 Onarowsetofsize2,consistingofrows7and8

PRIOR Onrow6

ROWSETSTARTINGATABSOLUTE13 FOR5ROWS

Onarowsetofsize3,consistingofrows13,14, and15

FIRSTROWSET Onarowsetofsize5,consistingofrows1,2,3,4, and5

Note: FormoreinformationabouttheFORnROWSclauseandtheROWSETclause,seethe topic“FETCH”inDB2SQLReference.

Dalam dokumen Application Programming and SQL Guide (Halaman 176-181)