1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>FETCH</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-explain.html" title="EXPLAIN" /><link rel="next" href="sql-grant.html" title="GRANT" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">FETCH</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-explain.html" title="EXPLAIN">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-grant.html" title="GRANT">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-FETCH"><div class="titlepage"></div><a id="id-1.9.3.149.1" class="indexterm"></a><a id="id-1.9.3.149.2" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">FETCH</span></h2><p>FETCH — retrieve rows from a query using a cursor</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 FETCH [ <em class="replaceable"><code>direction</code></em> ] [ FROM | IN ] <em class="replaceable"><code>cursor_name</code></em>
5 <span class="phrase">where <em class="replaceable"><code>direction</code></em> can be one of:</span>
11 ABSOLUTE <em class="replaceable"><code>count</code></em>
12 RELATIVE <em class="replaceable"><code>count</code></em>
13 <em class="replaceable"><code>count</code></em>
16 FORWARD <em class="replaceable"><code>count</code></em>
19 BACKWARD <em class="replaceable"><code>count</code></em>
21 </pre></div><div class="refsect1" id="id-1.9.3.149.6"><h2>Description</h2><p>
22 <code class="command">FETCH</code> retrieves rows using a previously-created cursor.
24 A cursor has an associated position, which is used by
25 <code class="command">FETCH</code>. The cursor position can be before the first row of the
26 query result, on any particular row of the result, or after the last row
27 of the result. When created, a cursor is positioned before the first row.
28 After fetching some rows, the cursor is positioned on the row most recently
29 retrieved. If <code class="command">FETCH</code> runs off the end of the available rows
30 then the cursor is left positioned after the last row, or before the first
31 row if fetching backward. <code class="command">FETCH ALL</code> or <code class="command">FETCH BACKWARD
32 ALL</code> will always leave the cursor positioned after the last row or before
35 The forms <code class="literal">NEXT</code>, <code class="literal">PRIOR</code>, <code class="literal">FIRST</code>,
36 <code class="literal">LAST</code>, <code class="literal">ABSOLUTE</code>, <code class="literal">RELATIVE</code> fetch
37 a single row after moving the cursor appropriately. If there is no
38 such row, an empty result is returned, and the cursor is left
39 positioned before the first row or after the last row as
42 The forms using <code class="literal">FORWARD</code> and <code class="literal">BACKWARD</code>
43 retrieve the indicated number of rows moving in the forward or
44 backward direction, leaving the cursor positioned on the
45 last-returned row (or after/before all rows, if the <em class="replaceable"><code>count</code></em> exceeds the number of rows
48 <code class="literal">RELATIVE 0</code>, <code class="literal">FORWARD 0</code>, and
49 <code class="literal">BACKWARD 0</code> all request fetching the current row without
50 moving the cursor, that is, re-fetching the most recently fetched
51 row. This will succeed unless the cursor is positioned before the
52 first row or after the last row; in which case, no row is returned.
53 </p><div class="note"><h3 class="title">Note</h3><p>
54 This page describes usage of cursors at the SQL command level.
55 If you are trying to use cursors inside a <span class="application">PL/pgSQL</span>
56 function, the rules are different —
57 see <a class="xref" href="plpgsql-cursors.html#PLPGSQL-CURSOR-USING" title="41.7.3. Using Cursors">Section 41.7.3</a>.
58 </p></div></div><div class="refsect1" id="id-1.9.3.149.7"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>direction</code></em></span></dt><dd><p><em class="replaceable"><code>direction</code></em> defines
59 the fetch direction and number of rows to fetch. It can be one
62 </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">NEXT</code></span></dt><dd><p>
63 Fetch the next row. This is the default if <em class="replaceable"><code>direction</code></em> is omitted.
64 </p></dd><dt><span class="term"><code class="literal">PRIOR</code></span></dt><dd><p>
66 </p></dd><dt><span class="term"><code class="literal">FIRST</code></span></dt><dd><p>
67 Fetch the first row of the query (same as <code class="literal">ABSOLUTE 1</code>).
68 </p></dd><dt><span class="term"><code class="literal">LAST</code></span></dt><dd><p>
69 Fetch the last row of the query (same as <code class="literal">ABSOLUTE -1</code>).
70 </p></dd><dt><span class="term"><code class="literal">ABSOLUTE <em class="replaceable"><code>count</code></em></code></span></dt><dd><p>
71 Fetch the <em class="replaceable"><code>count</code></em>'th row of the query,
72 or the <code class="literal">abs(<em class="replaceable"><code>count</code></em>)</code>'th row from
73 the end if <em class="replaceable"><code>count</code></em> is negative. Position
74 before first row or after last row if <em class="replaceable"><code>count</code></em> is out of range; in
75 particular, <code class="literal">ABSOLUTE 0</code> positions before
77 </p></dd><dt><span class="term"><code class="literal">RELATIVE <em class="replaceable"><code>count</code></em></code></span></dt><dd><p>
78 Fetch the <em class="replaceable"><code>count</code></em>'th succeeding row, or
79 the <code class="literal">abs(<em class="replaceable"><code>count</code></em>)</code>'th prior
80 row if <em class="replaceable"><code>count</code></em> is
81 negative. <code class="literal">RELATIVE 0</code> re-fetches the
83 </p></dd><dt><span class="term"><em class="replaceable"><code>count</code></em></span></dt><dd><p>
84 Fetch the next <em class="replaceable"><code>count</code></em> rows (same as
85 <code class="literal">FORWARD <em class="replaceable"><code>count</code></em></code>).
86 </p></dd><dt><span class="term"><code class="literal">ALL</code></span></dt><dd><p>
87 Fetch all remaining rows (same as <code class="literal">FORWARD ALL</code>).
88 </p></dd><dt><span class="term"><code class="literal">FORWARD</code></span></dt><dd><p>
89 Fetch the next row (same as <code class="literal">NEXT</code>).
90 </p></dd><dt><span class="term"><code class="literal">FORWARD <em class="replaceable"><code>count</code></em></code></span></dt><dd><p>
91 Fetch the next <em class="replaceable"><code>count</code></em> rows.
92 <code class="literal">FORWARD 0</code> re-fetches the current row.
93 </p></dd><dt><span class="term"><code class="literal">FORWARD ALL</code></span></dt><dd><p>
94 Fetch all remaining rows.
95 </p></dd><dt><span class="term"><code class="literal">BACKWARD</code></span></dt><dd><p>
96 Fetch the prior row (same as <code class="literal">PRIOR</code>).
97 </p></dd><dt><span class="term"><code class="literal">BACKWARD <em class="replaceable"><code>count</code></em></code></span></dt><dd><p>
98 Fetch the prior <em class="replaceable"><code>count</code></em> rows (scanning
99 backwards). <code class="literal">BACKWARD 0</code> re-fetches the
101 </p></dd><dt><span class="term"><code class="literal">BACKWARD ALL</code></span></dt><dd><p>
102 Fetch all prior rows (scanning backwards).
103 </p></dd></dl></div></dd><dt><span class="term"><em class="replaceable"><code>count</code></em></span></dt><dd><p><em class="replaceable"><code>count</code></em> is a
104 possibly-signed integer constant, determining the location or
105 number of rows to fetch. For <code class="literal">FORWARD</code> and
106 <code class="literal">BACKWARD</code> cases, specifying a negative <em class="replaceable"><code>count</code></em> is equivalent to changing
107 the sense of <code class="literal">FORWARD</code> and <code class="literal">BACKWARD</code>.
108 </p></dd><dt><span class="term"><em class="replaceable"><code>cursor_name</code></em></span></dt><dd><p>
109 An open cursor's name.
110 </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.149.8"><h2>Outputs</h2><p>
111 On successful completion, a <code class="command">FETCH</code> command returns a command
113 </p><pre class="screen">
114 FETCH <em class="replaceable"><code>count</code></em>
116 The <em class="replaceable"><code>count</code></em> is the number
117 of rows fetched (possibly zero). Note that in
118 <span class="application">psql</span>, the command tag will not actually be
119 displayed, since <span class="application">psql</span> displays the fetched
121 </p></div><div class="refsect1" id="id-1.9.3.149.9"><h2>Notes</h2><p>
122 The cursor should be declared with the <code class="literal">SCROLL</code>
123 option if one intends to use any variants of <code class="command">FETCH</code>
124 other than <code class="command">FETCH NEXT</code> or <code class="command">FETCH FORWARD</code> with
125 a positive count. For simple queries
126 <span class="productname">PostgreSQL</span> will allow backwards fetch
127 from cursors not declared with <code class="literal">SCROLL</code>, but this
128 behavior is best not relied on. If the cursor is declared with
129 <code class="literal">NO SCROLL</code>, no backward fetches are allowed.
131 <code class="literal">ABSOLUTE</code> fetches are not any faster than
132 navigating to the desired row with a relative move: the underlying
133 implementation must traverse all the intermediate rows anyway.
134 Negative absolute fetches are even worse: the query must be read to
135 the end to find the last row, and then traversed backward from
136 there. However, rewinding to the start of the query (as with
137 <code class="literal">FETCH ABSOLUTE 0</code>) is fast.
139 <a class="link" href="sql-declare.html" title="DECLARE"><code class="command">DECLARE</code></a>
140 is used to define a cursor. Use
141 <a class="link" href="sql-move.html" title="MOVE"><code class="command">MOVE</code></a>
142 to change cursor position without retrieving data.
143 </p></div><div class="refsect1" id="id-1.9.3.149.10"><h2>Examples</h2><p>
144 The following example traverses a table using a cursor:
146 </p><pre class="programlisting">
150 DECLARE liahona SCROLL CURSOR FOR SELECT * FROM films;
152 -- Fetch the first 5 rows in the cursor liahona:
153 FETCH FORWARD 5 FROM liahona;
155 code | title | did | date_prod | kind | len
156 -------+-------------------------+-----+------------+----------+-------
157 BL101 | The Third Man | 101 | 1949-12-23 | Drama | 01:44
158 BL102 | The African Queen | 101 | 1951-08-11 | Romantic | 01:43
159 JL201 | Une Femme est une Femme | 102 | 1961-03-12 | Romantic | 01:25
160 P_301 | Vertigo | 103 | 1958-11-14 | Action | 02:08
161 P_302 | Becket | 103 | 1964-02-03 | Drama | 02:28
163 -- Fetch the previous row:
164 FETCH PRIOR FROM liahona;
166 code | title | did | date_prod | kind | len
167 -------+---------+-----+------------+--------+-------
168 P_301 | Vertigo | 103 | 1958-11-14 | Action | 02:08
170 -- Close the cursor and end the transaction:
173 </pre></div><div class="refsect1" id="id-1.9.3.149.11"><h2>Compatibility</h2><p>
174 The SQL standard defines <code class="command">FETCH</code> for use in
175 embedded SQL only. The variant of <code class="command">FETCH</code>
176 described here returns the data as if it were a
177 <code class="command">SELECT</code> result rather than placing it in host
178 variables. Other than this point, <code class="command">FETCH</code> is
179 fully upward-compatible with the SQL standard.
181 The <code class="command">FETCH</code> forms involving
182 <code class="literal">FORWARD</code> and <code class="literal">BACKWARD</code>, as well
183 as the forms <code class="literal">FETCH <em class="replaceable"><code>count</code></em></code> and <code class="literal">FETCH
184 ALL</code>, in which <code class="literal">FORWARD</code> is implicit, are
185 <span class="productname">PostgreSQL</span> extensions.
187 The SQL standard allows only <code class="literal">FROM</code> preceding the cursor
188 name; the option to use <code class="literal">IN</code>, or to leave them out altogether, is
190 </p></div><div class="refsect1" id="id-1.9.3.149.12"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-close.html" title="CLOSE"><span class="refentrytitle">CLOSE</span></a>, <a class="xref" href="sql-declare.html" title="DECLARE"><span class="refentrytitle">DECLARE</span></a>, <a class="xref" href="sql-move.html" title="MOVE"><span class="refentrytitle">MOVE</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-explain.html" title="EXPLAIN">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-grant.html" title="GRANT">Next</a></td></tr><tr><td width="40%" align="left" valign="top">EXPLAIN </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> GRANT</td></tr></table></div></body></html>