FETCH - from a query using a cursor fetch row

SYNOPSIS

 

FETCH [ direction { FROM | IN } ] cursorname

where direction can be empty or one of:

    NEXT
    PRIOR
    FIRST
    LAST
    ABSOLUTE count
    RELATIVE count
    count
    ALL
    FORWARD
    FORWARD count
    FORWARD ALL
    BACKWARD
    BACKWARD count
    BACKWARD ALL

DESCRIPTION Description

FETCH using a cursor to retrieve rows.


 A cursor has a location associated with the use of FETCH. Cursor position can be before the first row of the query result, or an arbitrary row in the result, or after the last row of the result. After you've created, the cursor is positioned before the first row. After fetching some rows, the cursor on the last row retrieved. If FETCH grab all available over the line, then it stopped after the last row, or in the case of forward arrested was parked in front of the first row. FETCH ALL or FETCH BACKWARD ALL will always leave the cursor positioned in front of the last row or the first row.

NEXT, PRIOR, FIRST, LAST, ABSOLUTE, RELATIVE form after the cursor to move on a line fetch. If there are no rows of data, then returns an empty result, the cursor will stop after the last row of the query results or before the first row.

FORWARD and BACKWARD fetch form during the specified number of lines forward or backward, and then the cursor is positioned on the return line of the last (or, if the count is greater than after a number of lines available, or until all rows. )

RELATIVE 0, FORWARD 0, and BACKWARD 0 all request fetching without moving the cursor's current line --- that is, re-fetching the most recently fetched row. After unless the cursor is positioned before the first or the last line, this action should be successful, and that in both cases, no rows are returned.

PARAMETERS Parameters

direction
direction direction defines the fetch and fetched rows. It may be one of:
NEXT

 Fetch the next line. When this is the default direction is omitted.
PRIOR

 Fetch the prior row.
FIRST

 The first row (same as ABSOLUTE 1) gripping the query.
LAST

 The last line of crawl queries (and the same as ABSOLUTE -1).
ABSOLUTE count

 Gripping the first query row count, or, if the count <0, the query results from the gripping end of the abs (count) line. If the count exceeds the range, the row is positioned before the first and after the last row position; especially ABSOLUTE 0 positions before the first row.
RELATIVE count
The first subsequent line fetch count, or, if the count <0 when the gripping of the preceding abs (count) line. If there is data, then, RELATIVE 0 re-fetches the current row.
count

 Fetch the next count rows (same as FORWARD count).
ALL

 Fetch all remaining rows (same as FORWARD ALL).
FORWARD

 Fetch the next row (same as NEXT).
FORWARD count

 Crawl count rows below. FORWARD 0 re-fetches the current row.
FORWARD ALL

 Fetch all remaining rows.
BACKWARD

 Fetch the prior row (same as PRIOR).
BACKWARD count

 Fetch the prior count rows (backward scan). BACKWARD 0 re-fetches the current row.
BACKWARD ALL

 Fetch all prior rows (scanning backwards).

 

count
count is a possibly-signed integer constant, determine the number of rows and the direction you want to crawl. For FORWARD and BACKWARD, declare a negative count is equivalent to changing the direction of FORWARD and BACKWARD.
cursorname

 An open cursor's name.

OUTPUTS Output


 Upon successful completion, a FETCH command returns a labeled form as the following

 

FETCH count


 The count is the number of rows fetched (possibly zero). Note that in psql, the command tag will not actually be displayed, since psql displays the fetched rows of.

NOTES Note


 If you want to use any variants of FETCH other than FETCH NEXT, or FETCH FORWARD with a positive count. It defines the cursor should be declared with the SCROLL option. For simple queries, PostgreSQL will allow those who have not declared with SCROLL cursors can also be reversed to crawl, but we'd better not rely on this behavior. If the cursor is defined with NO SCROLL, no backward fetches.

ABSOLUTE fetches are not faster than with a relative move to the desired data row: the underlying implementation must traverse all the intermediate rows. Negative absolute fetches are even worse: the query must be read to the end to find the last row, and then traversed backward from there. However, to fall back to the beginning of the query (as FETCH ABSOLUTE 0) soon.


 Update data in a cursor is not supported by PostgreSQL.

The DECLARE [ DECLARE (. 7)] is used to define a cursor. Using the MOVE [ Move (. 7)] to change cursor position without retrieving data.

EXAMPLES Examples


 The following example using a cursor across a table.

 

BEGIN WORK;

- the establishment of a cursor:
DECLARE liahona SCROLL CURSOR FOR SELECT * FROM films;

- 5 to grab the first row 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 | A Woman is a Woman | 102 | 1961-03-12 | Romantic | 1:25
 P_301 | Vertigo | 103 | 14/11/1958 | Action | 2:08
 P_302 | Becket                  | 103 | 1964-02-03 | Drama    | 02:28

- Fetch the prior row:
FETCH PRIOR FROM liahona;

 code  |  title  | did | date_prod  |  kind  |  len
-------+---------+-----+------------+--------+-------
 P_301 | Vertigo | 103 | 14/11/1958 | Action | 2:08

- Close the cursor and commit the transaction:
CLOSE liahona;
COMMIT WORK;

COMPATIBILITY The

SQL standard defines FETCH only for the embedded environment. Variant of FETCH described here is returned as the result data as SELECT result rather than placing it in host variable. In addition to this point, FETCH is fully upward-compatible with the SQL standard.


 Involving FORWARD and BACKWARD FETCH forms (including forms FETCH count and FETCH ALL, this time FORWARD is implied) is a PostgreSQL extension.


 SQL standard allows only preceding the cursor FROM, IN with an extension. 

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11082283.html