fetch.7

来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 228 行

7
228
字号
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "FETCH" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMEFETCH \- retrieve rows from a query using a cursor.SH SYNOPSIS.sp.nfFETCH [ \fIdirection\fR { FROM | IN } ] \fIcursorname\fRwhere \fIdirection\fR can be empty or one of:    NEXT    PRIOR    FIRST    LAST    ABSOLUTE \fIcount\fR    RELATIVE \fIcount\fR    \fIcount\fR    ALL    FORWARD    FORWARD \fIcount\fR    FORWARD ALL    BACKWARD    BACKWARD \fIcount\fR    BACKWARD ALL.sp.fi.SH "DESCRIPTION".PP\fBFETCH\fR retrieves rows using a previously-created cursor..PPA cursor has an associated position, which is used by\fBFETCH\fR. The cursor position can be before the first row of thequery result, on any particular row of the result, or after the last rowof the result. When created, a cursor is positioned before the first row.After fetching some rows, the cursor is positioned on the row most recentlyretrieved. If \fBFETCH\fR runs off the end of the available rowsthen the cursor is left positioned after the last row, or before the firstrow if fetching backward. \fBFETCH ALL\fR or \fBFETCH BACKWARDALL\fR will always leave the cursor positioned after the last row or beforethe first row..PPThe forms NEXT, PRIOR, FIRST,LAST, ABSOLUTE, RELATIVE fetcha single row after moving the cursor appropriately. If there is nosuch row, an empty result is returned, and the cursor is leftpositioned before the first row or after the last row asappropriate..PPThe forms using FORWARD and BACKWARDretrieve the indicated number of rows moving in the forward orbackward direction, leaving the cursor positioned on thelast-returned row (or after/before all rows, if the \fIcount\fR exceeds the number of rowsavailable)..PPRELATIVE 0, FORWARD 0, andBACKWARD 0 all request fetching the current row withoutmoving the cursor, that is, re-fetching the most recently fetchedrow. This will succeed unless the cursor is positioned before thefirst row or after the last row; in which case, no row is returned..SH "PARAMETERS".TP\fB\fIdirection\fB\fR\fIdirection\fR definesthe fetch direction and number of rows to fetch. It can be oneof the following:.RS.TP\fBNEXT\fRFetch the next row. This is the default if \fIdirection\fR is omitted..TP\fBPRIOR\fRFetch the prior row..TP\fBFIRST\fRFetch the first row of the query (same as ABSOLUTE 1)..TP\fBLAST\fRFetch the last row of the query (same as ABSOLUTE -1)..TP\fBABSOLUTE \fIcount\fB\fRFetch the \fIcount\fR'th row of the query,or the abs(\fIcount\fR)'th row fromthe end if \fIcount\fR is negative. Positionbefore first row or after last row if \fIcount\fR is out of range; inparticular, ABSOLUTE 0 positions beforethe first row..TP\fBRELATIVE \fIcount\fB\fRFetch the \fIcount\fR'th succeeding row, orthe abs(\fIcount\fR)'th priorrow if \fIcount\fR isnegative. RELATIVE 0 re-fetches thecurrent row, if any..TP\fB\fIcount\fB\fRFetch the next \fIcount\fR rows (same asFORWARD \fIcount\fR)..TP\fBALL\fRFetch all remaining rows (same as FORWARD ALL)..TP\fBFORWARD\fRFetch the next row (same as NEXT)..TP\fBFORWARD \fIcount\fB\fRFetch the next \fIcount\fR rows.FORWARD 0 re-fetches the current row..TP\fBFORWARD ALL\fRFetch all remaining rows..TP\fBBACKWARD\fRFetch the prior row (same as PRIOR)..TP\fBBACKWARD \fIcount\fB\fRFetch the prior \fIcount\fR rows (scanningbackwards). BACKWARD 0 re-fetches thecurrent row..TP\fBBACKWARD ALL\fRFetch all prior rows (scanning backwards)..RE.PP.TP\fB\fIcount\fB\fR\fIcount\fR is apossibly-signed integer constant, determining the location ornumber of rows to fetch. For FORWARD andBACKWARD cases, specifying a negative \fIcount\fR is equivalent to changingthe sense of FORWARD and BACKWARD..TP\fB\fIcursorname\fB\fRAn open cursor's name..SH "OUTPUTS".PPOn successful completion, a \fBFETCH\fR command returns a commandtag of the form.sp.nfFETCH \fIcount\fR.sp.fiThe \fIcount\fR is the numberof rows fetched (possibly zero). Note that in\fBpsql\fR, the command tag will not actually bedisplayed, since \fBpsql\fR displays the fetchedrows instead..SH "NOTES".PPThe cursor should be declared with the SCROLLoption if one intends to use any variants of \fBFETCH\fRother than \fBFETCH NEXT\fR or \fBFETCH FORWARD\fR witha positive count. For simple queriesPostgreSQL will allow backwards fetchfrom cursors not declared with SCROLL, but thisbehavior is best not relied on. If the cursor is declared withNO SCROLL, no backward fetches are allowed..PPABSOLUTE fetches are not any faster thannavigating to the desired row with a relative move: the underlyingimplementation must traverse all the intermediate rows anyway.Negative absolute fetches are even worse: the query must be read tothe end to find the last row, and then traversed backward fromthere. However, rewinding to the start of the query (as withFETCH ABSOLUTE 0) is fast..PPUpdating data via a cursor is currently not supported byPostgreSQL..PPDECLARE [\fBdeclare\fR(7)]is used to define a cursor. UseMOVE [\fBmove\fR(7)]to change cursor position without retrieving data..SH "EXAMPLES".PPThe following example traverses a table using a cursor..sp.nfBEGIN WORK;-- Set up a cursor:DECLARE liahona SCROLL CURSOR FOR SELECT * FROM films;-- Fetch the first 5 rows in the cursor liahona:FETCH FORWARD 5 FROM liahona; code  |          title          | did | date_prod  |   kind   |  len-------+-------------------------+-----+------------+----------+------- BL101 | The Third Man           | 101 | 1949-12-23 | Drama    | 01:44 BL102 | The African Queen       | 101 | 1951-08-11 | Romantic | 01:43 JL201 | Une Femme est une Femme | 102 | 1961-03-12 | Romantic | 01:25 P_301 | Vertigo                 | 103 | 1958-11-14 | Action   | 02:08 P_302 | Becket                  | 103 | 1964-02-03 | Drama    | 02:28-- Fetch the previous row:FETCH PRIOR FROM liahona; code  |  title  | did | date_prod  |  kind  |  len-------+---------+-----+------------+--------+------- P_301 | Vertigo | 103 | 1958-11-14 | Action | 02:08-- Close the cursor and end the transaction:CLOSE liahona;COMMIT WORK;.sp.fi.SH "COMPATIBILITY".PPThe SQL standard defines \fBFETCH\fR for use inembedded SQL only. The variant of \fBFETCH\fRdescribed here returns the data as if it were a\fBSELECT\fR result rather than placing it in hostvariables. Other than this point, \fBFETCH\fR isfully upward-compatible with the SQL standard..PPThe \fBFETCH\fR forms involvingFORWARD and BACKWARD, as wellas the forms FETCH \fIcount\fR and FETCHALL, in which FORWARD is implicit, arePostgreSQL extensions..PPThe SQL standard allows only FROM preceding the cursorname; the option to use IN is an extension..SH "SEE ALSO"CLOSE [\fBclose\fR(7)], DECLARE [\fBdeclare\fR(l)], MOVE [\fBmove\fR(l)]

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?