1FETCH(7) SQL Commands FETCH(7)
2
3
4
6 FETCH - retrieve rows from a query using a cursor
7
8
10 FETCH [ direction { FROM | IN } ] cursorname
11
12 where direction can be empty or one of:
13
14 NEXT
15 PRIOR
16 FIRST
17 LAST
18 ABSOLUTE count
19 RELATIVE count
20 count
21 ALL
22 FORWARD
23 FORWARD count
24 FORWARD ALL
25 BACKWARD
26 BACKWARD count
27 BACKWARD ALL
28
29
31 FETCH retrieves rows using a previously-created cursor.
32
33 A cursor has an associated position, which is used by FETCH. The cursor
34 position can be before the first row of the query result, on any par‐
35 ticular row of the result, or after the last row of the result. When
36 created, a cursor is positioned before the first row. After fetching
37 some rows, the cursor is positioned on the row most recently retrieved.
38 If FETCH runs off the end of the available rows then the cursor is left
39 positioned after the last row, or before the first row if fetching
40 backward. FETCH ALL or FETCH BACKWARD ALL will always leave the cursor
41 positioned after the last row or before the first row.
42
43 The forms NEXT, PRIOR, FIRST, LAST, ABSOLUTE, RELATIVE fetch a single
44 row after moving the cursor appropriately. If there is no such row, an
45 empty result is returned, and the cursor is left positioned before the
46 first row or after the last row as appropriate.
47
48 The forms using FORWARD and BACKWARD retrieve the indicated number of
49 rows moving in the forward or backward direction, leaving the cursor
50 positioned on the last-returned row (or after/before all rows, if the
51 count exceeds the number of rows available).
52
53 RELATIVE 0, FORWARD 0, and BACKWARD 0 all request fetching the current
54 row without moving the cursor, that is, re-fetching the most recently
55 fetched row. This will succeed unless the cursor is positioned before
56 the first row or after the last row; in which case, no row is returned.
57
58 Note: This page describes usage of cursors at the SQL command
59 level. If you are trying to use cursors inside a PL/pgSQL func‐
60 tion, the rules are different — see in the documentation.
61
62
64 direction
65 direction defines the fetch direction and number of rows to
66 fetch. It can be one of the following:
67
68 NEXT Fetch the next row. This is the default if direction is
69 omitted.
70
71 PRIOR Fetch the prior row.
72
73 FIRST Fetch the first row of the query (same as ABSOLUTE 1).
74
75 LAST Fetch the last row of the query (same as ABSOLUTE -1).
76
77 ABSOLUTE count
78 Fetch the count'th row of the query, or the abs(count)'th
79 row from the end if count is negative. Position before
80 first row or after last row if count is out of range; in
81 particular, ABSOLUTE 0 positions before the first row.
82
83 RELATIVE count
84 Fetch the count'th succeeding row, or the abs(count)'th
85 prior row if count is negative. RELATIVE 0 re-fetches the
86 current row, if any.
87
88 count Fetch the next count rows (same as FORWARD count).
89
90 ALL Fetch all remaining rows (same as FORWARD ALL).
91
92 FORWARD
93 Fetch the next row (same as NEXT).
94
95 FORWARD count
96 Fetch the next count rows. FORWARD 0 re-fetches the cur‐
97 rent row.
98
99 FORWARD ALL
100 Fetch all remaining rows.
101
102 BACKWARD
103 Fetch the prior row (same as PRIOR).
104
105 BACKWARD count
106 Fetch the prior count rows (scanning backwards). BACKWARD
107 0 re-fetches the current row.
108
109 BACKWARD ALL
110 Fetch all prior rows (scanning backwards).
111
112 count count is a possibly-signed integer constant, determining the
113 location or number of rows to fetch. For FORWARD and BACKWARD
114 cases, specifying a negative count is equivalent to changing the
115 sense of FORWARD and BACKWARD.
116
117 cursorname
118 An open cursor's name.
119
121 On successful completion, a FETCH command returns a command tag of the
122 form
123
124 FETCH count
125
126 The count is the number of rows fetched (possibly zero). Note that in
127 psql, the command tag will not actually be displayed, since psql dis‐
128 plays the fetched rows instead.
129
131 The cursor should be declared with the SCROLL option if one intends to
132 use any variants of FETCH other than FETCH NEXT or FETCH FORWARD with a
133 positive count. For simple queries PostgreSQL will allow backwards
134 fetch from cursors not declared with SCROLL, but this behavior is best
135 not relied on. If the cursor is declared with NO SCROLL, no backward
136 fetches are allowed.
137
138 ABSOLUTE fetches are not any faster than navigating to the desired row
139 with a relative move: the underlying implementation must traverse all
140 the intermediate rows anyway. Negative absolute fetches are even
141 worse: the query must be read to the end to find the last row, and then
142 traversed backward from there. However, rewinding to the start of the
143 query (as with FETCH ABSOLUTE 0) is fast.
144
145 DECLARE [declare(7)] is used to define a cursor. Use MOVE [move(7)] to
146 change cursor position without retrieving data.
147
149 The following example traverses a table using a cursor:
150
151 BEGIN WORK;
152
153 -- Set up a cursor:
154 DECLARE liahona SCROLL CURSOR FOR SELECT * FROM films;
155
156 -- Fetch the first 5 rows in the cursor liahona:
157 FETCH FORWARD 5 FROM liahona;
158
159 code | title | did | date_prod | kind | len
160 -------+-------------------------+-----+------------+----------+-------
161 BL101 | The Third Man | 101 | 1949-12-23 | Drama | 01:44
162 BL102 | The African Queen | 101 | 1951-08-11 | Romantic | 01:43
163 JL201 | Une Femme est une Femme | 102 | 1961-03-12 | Romantic | 01:25
164 P_301 | Vertigo | 103 | 1958-11-14 | Action | 02:08
165 P_302 | Becket | 103 | 1964-02-03 | Drama | 02:28
166
167 -- Fetch the previous row:
168 FETCH PRIOR FROM liahona;
169
170 code | title | did | date_prod | kind | len
171 -------+---------+-----+------------+--------+-------
172 P_301 | Vertigo | 103 | 1958-11-14 | Action | 02:08
173
174 -- Close the cursor and end the transaction:
175 CLOSE liahona;
176 COMMIT WORK;
177
178
180 The SQL standard defines FETCH for use in embedded SQL only. The vari‐
181 ant of FETCH described here returns the data as if it were a SELECT
182 result rather than placing it in host variables. Other than this point,
183 FETCH is fully upward-compatible with the SQL standard.
184
185 The FETCH forms involving FORWARD and BACKWARD, as well as the forms
186 FETCH count and FETCH ALL, in which FORWARD is implicit, are PostgreSQL
187 extensions.
188
189 The SQL standard allows only FROM preceding the cursor name; the option
190 to use IN is an extension.
191
193 CLOSE [close(7)], DECLARE [declare(7)], MOVE [move(7)]
194
195
196
197SQL - Language Statements 2011-09-22 FETCH(7)