3 PostgreSQL documentation
6 <refentry id=
"SQL-FETCH">
8 <refentrytitle id=
"SQL-FETCH-TITLE">FETCH
</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
14 <refname>FETCH
</refname>
15 <refpurpose>retrieve rows from a query using a cursor
</refpurpose>
18 <indexterm zone=
"sql-fetch">
19 <primary>FETCH
</primary>
22 <indexterm zone=
"sql-fetch">
23 <primary>cursor
</primary>
24 <secondary>FETCH
</secondary>
29 FETCH [
<replaceable class=
"PARAMETER">direction
</replaceable> { FROM | IN } ]
<replaceable class=
"PARAMETER">cursorname
</replaceable>
31 where
<replaceable class=
"PARAMETER">direction
</replaceable> can be empty or one of:
37 ABSOLUTE
<replaceable class=
"PARAMETER">count
</replaceable>
38 RELATIVE
<replaceable class=
"PARAMETER">count
</replaceable>
39 <replaceable class=
"PARAMETER">count
</replaceable>
42 FORWARD
<replaceable class=
"PARAMETER">count
</replaceable>
45 BACKWARD
<replaceable class=
"PARAMETER">count
</replaceable>
51 <title>Description
</title>
54 <command>FETCH
</command> retrieves rows using a previously-created cursor.
58 A cursor has an associated position, which is used by
59 <command>FETCH<
/>. The cursor position can be before the first row of the
60 query result, on any particular row of the result, or after the last row
61 of the result. When created, a cursor is positioned before the first row.
62 After fetching some rows, the cursor is positioned on the row most recently
63 retrieved. If
<command>FETCH<
/> runs off the end of the available rows
64 then the cursor is left positioned after the last row, or before the first
65 row if fetching backward.
<command>FETCH ALL<
/> or
<command>FETCH BACKWARD
66 ALL<
/> will always leave the cursor positioned after the last row or before
71 The forms
<literal>NEXT<
/>,
<literal>PRIOR<
/>,
<literal>FIRST<
/>,
72 <literal>LAST<
/>,
<literal>ABSOLUTE<
/>,
<literal>RELATIVE<
/> fetch
73 a single row after moving the cursor appropriately. If there is no
74 such row, an empty result is returned, and the cursor is left
75 positioned before the first row or after the last row as
80 The forms using
<literal>FORWARD<
/> and
<literal>BACKWARD<
/>
81 retrieve the indicated number of rows moving in the forward or
82 backward direction, leaving the cursor positioned on the
83 last-returned row (or after/before all rows, if the
<replaceable
84 class=
"PARAMETER">count
</replaceable> exceeds the number of rows
89 <literal>RELATIVE
0<
/>,
<literal>FORWARD
0<
/>, and
90 <literal>BACKWARD
0<
/> all request fetching the current row without
91 moving the cursor, that is, re-fetching the most recently fetched
92 row. This will succeed unless the cursor is positioned before the
93 first row or after the last row; in which case, no row is returned.
98 This page describes usage of cursors at the SQL command level.
99 If you are trying to use cursors inside a
<application>PL/pgSQL<
/>
100 function, the rules are different
—
101 see
<xref linkend=
"plpgsql-cursors">.
107 <title>Parameters
</title>
111 <term><replaceable class=
"PARAMETER">direction
</replaceable></term>
114 <replaceable class=
"PARAMETER">direction
</replaceable> defines
115 the fetch direction and number of rows to fetch. It can be one
120 <term><literal>NEXT
</literal></term>
123 Fetch the next row. This is the default if
<replaceable
124 class=
"PARAMETER">direction
</replaceable> is omitted.
130 <term><literal>PRIOR
</literal></term>
139 <term><literal>FIRST
</literal></term>
142 Fetch the first row of the query (same as
<literal>ABSOLUTE
1</literal>).
148 <term><literal>LAST
</literal></term>
151 Fetch the last row of the query (same as
<literal>ABSOLUTE -
1</literal>).
157 <term><literal>ABSOLUTE
<replaceable class=
"PARAMETER">count
</replaceable></literal></term>
160 Fetch the
<replaceable
161 class=
"PARAMETER">count
</replaceable>'th row of the query,
162 or the
<literal>abs(
<replaceable
163 class=
"PARAMETER">count
</replaceable>)
</literal>'th row from
164 the end if
<replaceable
165 class=
"PARAMETER">count
</replaceable> is negative. Position
166 before first row or after last row if
<replaceable
167 class=
"PARAMETER">count
</replaceable> is out of range; in
168 particular,
<literal>ABSOLUTE
0</literal> positions before
175 <term><literal>RELATIVE
<replaceable class=
"PARAMETER">count
</replaceable></literal></term>
178 Fetch the
<replaceable
179 class=
"PARAMETER">count
</replaceable>'th succeeding row, or
180 the
<literal>abs(
<replaceable
181 class=
"PARAMETER">count
</replaceable>)
</literal>'th prior
182 row if
<replaceable class=
"PARAMETER">count
</replaceable> is
183 negative.
<literal>RELATIVE
0</literal> re-fetches the
190 <term><replaceable class=
"PARAMETER">count
</replaceable></term>
193 Fetch the next
<replaceable
194 class=
"PARAMETER">count
</replaceable> rows (same as
195 <literal>FORWARD
<replaceable
196 class=
"PARAMETER">count
</replaceable></literal>).
202 <term><literal>ALL
</literal></term>
205 Fetch all remaining rows (same as
<literal>FORWARD ALL
</literal>).
211 <term><literal>FORWARD
</literal></term>
214 Fetch the next row (same as
<literal>NEXT
</literal>).
220 <term><literal>FORWARD
<replaceable class=
"PARAMETER">count
</replaceable></literal></term>
223 Fetch the next
<replaceable
224 class=
"PARAMETER">count
</replaceable> rows.
225 <literal>FORWARD
0</literal> re-fetches the current row.
231 <term><literal>FORWARD ALL
</literal></term>
234 Fetch all remaining rows.
240 <term><literal>BACKWARD
</literal></term>
243 Fetch the prior row (same as
<literal>PRIOR
</literal>).
249 <term><literal>BACKWARD
<replaceable class=
"PARAMETER">count
</replaceable></literal></term>
252 Fetch the prior
<replaceable
253 class=
"PARAMETER">count
</replaceable> rows (scanning
254 backwards).
<literal>BACKWARD
0</literal> re-fetches the
261 <term><literal>BACKWARD ALL
</literal></term>
264 Fetch all prior rows (scanning backwards).
274 <term><replaceable class=
"PARAMETER">count
</replaceable></term>
277 <replaceable class=
"PARAMETER">count
</replaceable> is a
278 possibly-signed integer constant, determining the location or
279 number of rows to fetch. For
<literal>FORWARD<
/> and
280 <literal>BACKWARD<
/> cases, specifying a negative
<replaceable
281 class=
"PARAMETER">count
</replaceable> is equivalent to changing
282 the sense of
<literal>FORWARD<
/> and
<literal>BACKWARD<
/>.
288 <term><replaceable class=
"PARAMETER">cursorname
</replaceable></term>
291 An open cursor's name.
299 <title>Outputs
</title>
302 On successful completion, a
<command>FETCH<
/> command returns a command
305 FETCH
<replaceable class=
"parameter">count
</replaceable>
307 The
<replaceable class=
"parameter">count
</replaceable> is the number
308 of rows fetched (possibly zero). Note that in
309 <application>psql
</application>, the command tag will not actually be
310 displayed, since
<application>psql
</application> displays the fetched
319 The cursor should be declared with the
<literal>SCROLL
</literal>
320 option if one intends to use any variants of
<command>FETCH<
/>
321 other than
<command>FETCH NEXT<
/> or
<command>FETCH FORWARD<
/> with
322 a positive count. For simple queries
323 <productname>PostgreSQL
</productname> will allow backwards fetch
324 from cursors not declared with
<literal>SCROLL
</literal>, but this
325 behavior is best not relied on. If the cursor is declared with
326 <literal>NO SCROLL
</literal>, no backward fetches are allowed.
330 <literal>ABSOLUTE
</literal> fetches are not any faster than
331 navigating to the desired row with a relative move: the underlying
332 implementation must traverse all the intermediate rows anyway.
333 Negative absolute fetches are even worse: the query must be read to
334 the end to find the last row, and then traversed backward from
335 there. However, rewinding to the start of the query (as with
336 <literal>FETCH ABSOLUTE
0</literal>) is fast.
340 <xref linkend=
"sql-declare" endterm=
"sql-declare-title">
341 is used to define a cursor. Use
342 <xref linkend=
"sql-move" endterm=
"sql-move-title">
343 to change cursor position without retrieving data.
348 <title>Examples
</title>
351 The following example traverses a table using a cursor:
357 DECLARE liahona SCROLL CURSOR FOR SELECT * FROM films;
359 -- Fetch the first
5 rows in the cursor liahona:
360 FETCH FORWARD
5 FROM liahona;
362 code | title | did | date_prod | kind | len
363 -------+-------------------------+-----+------------+----------+-------
364 BL101 | The Third Man |
101 |
1949-
12-
23 | Drama |
01:
44
365 BL102 | The African Queen |
101 |
1951-
08-
11 | Romantic |
01:
43
366 JL201 | Une Femme est une Femme |
102 |
1961-
03-
12 | Romantic |
01:
25
367 P_301 | Vertigo |
103 |
1958-
11-
14 | Action |
02:
08
368 P_302 | Becket |
103 |
1964-
02-
03 | Drama |
02:
28
370 -- Fetch the previous row:
371 FETCH PRIOR FROM liahona;
373 code | title | did | date_prod | kind | len
374 -------+---------+-----+------------+--------+-------
375 P_301 | Vertigo |
103 |
1958-
11-
14 | Action |
02:
08
377 -- Close the cursor and end the transaction:
385 <title>Compatibility
</title>
388 The SQL standard defines
<command>FETCH
</command> for use in
389 embedded SQL only. The variant of
<command>FETCH
</command>
390 described here returns the data as if it were a
391 <command>SELECT
</command> result rather than placing it in host
392 variables. Other than this point,
<command>FETCH
</command> is
393 fully upward-compatible with the SQL standard.
397 The
<command>FETCH
</command> forms involving
398 <literal>FORWARD
</literal> and
<literal>BACKWARD
</literal>, as well
399 as the forms
<literal>FETCH
<replaceable
400 class=
"PARAMETER">count
</replaceable></literal> and
<literal>FETCH
401 ALL
</literal>, in which
<literal>FORWARD
</literal> is implicit, are
402 <productname>PostgreSQL
</productname> extensions.
406 The SQL standard allows only
<literal>FROM<
/> preceding the cursor
407 name; the option to use
<literal>IN<
/> is an extension.
412 <title>See Also
</title>
414 <simplelist type=
"inline">
415 <member><xref linkend=
"sql-close" endterm=
"sql-close-title"></member>
416 <member><xref linkend=
"sql-declare" endterm=
"sql-declare-title"></member>
417 <member><xref linkend=
"sql-move" endterm=
"sql-move-title"></member>