FETCH SQL - Language Statements FETCH retrieve rows from a table using a cursor 1999-07-20 FETCH [ direction ] [ count ] { IN | FROM } cursor FETCH [ FORWARD | BACKWARD | RELATIVE ] [ # | ALL | NEXT | PRIOR ] { IN | FROM } cursor 1998-09-01 Inputs direction direction defines the fetch direction. It can be one of the following: FORWARD fetch next row(s). This is the default if direction is omitted. BACKWARD fetch previous row(s). RELATIVE Noise word for SQL92 compatibility. count count determines how many rows to fetch. It can be one of the following: # A signed integer constant that specifies how many rows to fetch. Note that a negative integer is equivalent to changing the sense of FORWARD and BACKWARD. Zero re-fetches the current row, if any. ALL Retrieve all remaining rows. NEXT Equivalent to specifying a count of 1. PRIOR Equivalent to specifying a count of -1. cursor An open cursor's name. 1998-04-15 Outputs FETCH returns rows from the result of the query defined by the specified cursor. The following messages will be returned if the query fails: WARNING: PerformPortalFetch: portal "cursor" not found If cursor is not previously declared. The cursor must be declared within a transaction block. WARNING: FETCH/ABSOLUTE not supported, using RELATIVE PostgreSQL does not support absolute positioning of cursors. 1998-04-15 Description FETCH allows a user to retrieve rows using a cursor. The number of rows retrieved is specified by #. If the number of rows remaining in the cursor is less than #, then only those available are fetched. Substituting the keyword ALL in place of a number will cause all remaining rows in the cursor to be retrieved. Rows may be fetched in both FORWARD and BACKWARD directions. The default direction is FORWARD. The cursor position can be before the first row of the query result, or on any particular row of the result, or after the last row of the result. When created, a cursor is positioned before the first row. After fetching some rows, the cursor is positioned on the last row retrieved. A new FETCH always steps one row in the specified direction (if possible) before beginning to return rows. If the FETCH requests more rows than available, the cursor is left positioned after the last row of the query result (or before the first row, in the case of a backward fetch). This will always be the case after FETCH ALL. A zero row count requests fetching the current row without moving the cursor --- that is, re-fetching the most recently fetched row. This will succeed unless the cursor is positioned before the first row or after the last row; in which case, no row is returned. Negative numbers are allowed to be specified for the row count. A negative number is equivalent to reversing the sense of the FORWARD and BACKWARD keywords. For example, FORWARD -1 is the same as BACKWARD 1. 1998-04-15 Notes Note that the FORWARD, BACKWARD, and ALL keywords are PostgreSQL extensions. See below for details on compatibility issues. Updating data in a cursor is not supported by PostgreSQL, because mapping cursor updates back to base tables is not generally possible, as is also the case with VIEW updates. Consequently, users must issue explicit UPDATE commands to replace data. Cursors may only be used inside of transactions because the data that they store spans multiple user queries. is used to define a cursor. Use to change cursor position without retrieving data. Refer to , , and for further information about transactions. Usage The following examples traverses a table using a cursor. -- Set up and use a cursor: BEGIN WORK; DECLARE liahona CURSOR FOR SELECT * FROM films; -- Fetch first 5 rows in the cursor liahona: FETCH FORWARD 5 IN 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 previous row: FETCH BACKWARD 1 IN liahona; code | title | did | date_prod | kind | len -------+---------+-----+------------+--------+------- P_301 | Vertigo | 103 | 1958-11-14 | Action | 02:08 -- close the cursor and commit work: CLOSE liahona; COMMIT WORK; Compatibility 1998-09-01 SQL92 The non-embedded use of cursors is a PostgreSQL extension. The syntax and usage of cursors is being compared against the embedded form of cursors defined in SQL92. SQL92 allows absolute positioning of the cursor for FETCH, and allows placing the results into explicit variables: FETCH ABSOLUTE # FROM cursor INTO :variable [, ...] ABSOLUTE The cursor should be positioned to the specified absolute row number. All row numbers in PostgreSQL are relative numbers so this capability is not supported. :variable Target host variable(s).