1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>FETCH</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="sql-commands.html" title="SQL Commands">
<link rel="prev" href="sql-explain.html" title="EXPLAIN">
<link rel="next" href="sql-grant.html" title="GRANT">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en">
<a name="sql-fetch"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2>Name</h2>
<p>FETCH — retrieve rows from a query using a cursor</p>
</div>
<a name="id774734"></a><a name="id774744"></a><div class="refsynopsisdiv">
<h2>Synopsis</h2>
<pre class="synopsis">FETCH [ <em class="replaceable"><code>direction</code></em> { FROM | IN } ] <em class="replaceable"><code>cursorname</code></em>
where <em class="replaceable"><code>direction</code></em> can be empty or one of:
NEXT
PRIOR
FIRST
LAST
ABSOLUTE <em class="replaceable"><code>count</code></em>
RELATIVE <em class="replaceable"><code>count</code></em>
<em class="replaceable"><code>count</code></em>
ALL
FORWARD
FORWARD <em class="replaceable"><code>count</code></em>
FORWARD ALL
BACKWARD
BACKWARD <em class="replaceable"><code>count</code></em>
BACKWARD ALL</pre>
</div>
<div class="refsect1" lang="en">
<a name="id774819"></a><h2>Description</h2>
<p> <code class="command">FETCH</code> retrieves rows using a previously-created cursor.
</p>
<p> A cursor has an associated position, which is used by
<code class="command">FETCH</code>. The cursor position can be before the first row of the
query result, 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 row most recently
retrieved. If <code class="command">FETCH</code> runs off the end of the available rows
then the cursor is left positioned after the last row, or before the first
row if fetching backward. <code class="command">FETCH ALL</code> or <code class="command">FETCH BACKWARD
ALL</code> will always leave the cursor positioned after the last row or before
the first row.
</p>
<p> The forms <code class="literal">NEXT</code>, <code class="literal">PRIOR</code>, <code class="literal">FIRST</code>,
<code class="literal">LAST</code>, <code class="literal">ABSOLUTE</code>, <code class="literal">RELATIVE</code> fetch
a single row after moving the cursor appropriately. If there is no
such row, an empty result is returned, and the cursor is left
positioned before the first row or after the last row as
appropriate.
</p>
<p> The forms using <code class="literal">FORWARD</code> and <code class="literal">BACKWARD</code>
retrieve the indicated number of rows moving in the forward or
backward direction, leaving the cursor positioned on the
last-returned row (or after/before all rows, if the <em class="replaceable"><code>count</code></em> exceeds the number of rows
available).
</p>
<p> <code class="literal">RELATIVE 0</code>, <code class="literal">FORWARD 0</code>, and
<code class="literal">BACKWARD 0</code> all request 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.
</p>
</div>
<div class="refsect1" lang="en">
<a name="id774970"></a><h2>Parameters</h2>
<div class="variablelist"><dl>
<dt><span class="term"><em class="replaceable"><code>direction</code></em></span></dt>
<dd>
<p> <em class="replaceable"><code>direction</code></em> defines
the fetch direction and number of rows to fetch. It can be one
of the following:
</p>
<div class="variablelist"><dl>
<dt><span class="term"><code class="literal">NEXT</code></span></dt>
<dd><p> Fetch the next row. This is the default if <em class="replaceable"><code>direction</code></em> is omitted.
</p></dd>
<dt><span class="term"><code class="literal">PRIOR</code></span></dt>
<dd><p> Fetch the prior row.
</p></dd>
<dt><span class="term"><code class="literal">FIRST</code></span></dt>
<dd><p> Fetch the first row of the query (same as <code class="literal">ABSOLUTE 1</code>).
</p></dd>
<dt><span class="term"><code class="literal">LAST</code></span></dt>
<dd><p> Fetch the last row of the query (same as <code class="literal">ABSOLUTE -1</code>).
</p></dd>
<dt><span class="term"><code class="literal">ABSOLUTE <em class="replaceable"><code>count</code></em></code></span></dt>
<dd><p> Fetch the <em class="replaceable"><code>count</code></em>'th row of the query,
or the <code class="literal">abs(<em class="replaceable"><code>count</code></em>)</code>'th row from
the end if <em class="replaceable"><code>count</code></em> is negative. Position
before first row or after last row if <em class="replaceable"><code>count</code></em> is out of range; in
particular, <code class="literal">ABSOLUTE 0</code> positions before
the first row.
</p></dd>
<dt><span class="term"><code class="literal">RELATIVE <em class="replaceable"><code>count</code></em></code></span></dt>
<dd><p> Fetch the <em class="replaceable"><code>count</code></em>'th succeeding row, or
the <code class="literal">abs(<em class="replaceable"><code>count</code></em>)</code>'th prior
row if <em class="replaceable"><code>count</code></em> is
negative. <code class="literal">RELATIVE 0</code> re-fetches the
current row, if any.
</p></dd>
<dt><span class="term"><em class="replaceable"><code>count</code></em></span></dt>
<dd><p> Fetch the next <em class="replaceable"><code>count</code></em> rows (same as
<code class="literal">FORWARD <em class="replaceable"><code>count</code></em></code>).
</p></dd>
<dt><span class="term"><code class="literal">ALL</code></span></dt>
<dd><p> Fetch all remaining rows (same as <code class="literal">FORWARD ALL</code>).
</p></dd>
<dt><span class="term"><code class="literal">FORWARD</code></span></dt>
<dd><p> Fetch the next row (same as <code class="literal">NEXT</code>).
</p></dd>
<dt><span class="term"><code class="literal">FORWARD <em class="replaceable"><code>count</code></em></code></span></dt>
<dd><p> Fetch the next <em class="replaceable"><code>count</code></em> rows.
<code class="literal">FORWARD 0</code> re-fetches the current row.
</p></dd>
<dt><span class="term"><code class="literal">FORWARD ALL</code></span></dt>
<dd><p> Fetch all remaining rows.
</p></dd>
<dt><span class="term"><code class="literal">BACKWARD</code></span></dt>
<dd><p> Fetch the prior row (same as <code class="literal">PRIOR</code>).
</p></dd>
<dt><span class="term"><code class="literal">BACKWARD <em class="replaceable"><code>count</code></em></code></span></dt>
<dd><p> Fetch the prior <em class="replaceable"><code>count</code></em> rows (scanning
backwards). <code class="literal">BACKWARD 0</code> re-fetches the
current row.
</p></dd>
<dt><span class="term"><code class="literal">BACKWARD ALL</code></span></dt>
<dd><p> Fetch all prior rows (scanning backwards).
</p></dd>
</dl></div>
<p>
</p>
</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
possibly-signed integer constant, determining the location or
number of rows to fetch. For <code class="literal">FORWARD</code> and
<code class="literal">BACKWARD</code> cases, specifying a negative <em class="replaceable"><code>count</code></em> is equivalent to changing
the sense of <code class="literal">FORWARD</code> and <code class="literal">BACKWARD</code>.
</p></dd>
<dt><span class="term"><em class="replaceable"><code>cursorname</code></em></span></dt>
<dd><p> An open cursor's name.
</p></dd>
</dl></div>
</div>
<div class="refsect1" lang="en">
<a name="id775419"></a><h2>Outputs</h2>
<p> On successful completion, a <code class="command">FETCH</code> command returns a command
tag of the form
</p>
<pre class="screen">FETCH <em class="replaceable"><code>count</code></em></pre>
<p>
The <em class="replaceable"><code>count</code></em> is the number
of rows fetched (possibly zero). Note that in
<span class="application">psql</span>, the command tag will not actually be
displayed, since <span class="application">psql</span> displays the fetched
rows instead.
</p>
</div>
<div class="refsect1" lang="en">
<a name="id775468"></a><h2>Notes</h2>
<p> The cursor should be declared with the <code class="literal">SCROLL</code>
option if one intends to use any variants of <code class="command">FETCH</code>
other than <code class="command">FETCH NEXT</code> or <code class="command">FETCH FORWARD</code> with
a positive count. For simple queries
<span class="productname">PostgreSQL</span> will allow backwards fetch
from cursors not declared with <code class="literal">SCROLL</code>, but this
behavior is best not relied on. If the cursor is declared with
<code class="literal">NO SCROLL</code>, no backward fetches are allowed.
</p>
<p> <code class="literal">ABSOLUTE</code> fetches are not any faster than
navigating to the desired row with a relative move: the underlying
implementation must traverse all the intermediate rows anyway.
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, rewinding to the start of the query (as with
<code class="literal">FETCH ABSOLUTE 0</code>) is fast.
</p>
<p> Updating data via a cursor is currently not supported by
<span class="productname">PostgreSQL</span>.
</p>
<p> <a href="sql-declare.html">DECLARE</a>
is used to define a cursor. Use
<a href="sql-move.html">MOVE</a>
to change cursor position without retrieving data.
</p>
</div>
<div class="refsect1" lang="en">
<a name="id775584"></a><h2>Examples</h2>
<p> The following example traverses a table using a cursor.
</p>
<pre class="programlisting">BEGIN WORK;
-- Set up a cursor:
DECLARE liahona SCROLL CURSOR FOR SELECT * FROM films;
-- Fetch the first 5 rows 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 | 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 the previous row:
FETCH PRIOR FROM liahona;
code | title | did | date_prod | kind | len
-------+---------+-----+------------+--------+-------
P_301 | Vertigo | 103 | 1958-11-14 | Action | 02:08
-- Close the cursor and end the transaction:
CLOSE liahona;
COMMIT WORK;</pre>
<p>
</p>
</div>
<div class="refsect1" lang="en">
<a name="id775613"></a><h2>Compatibility</h2>
<p> The SQL standard defines <code class="command">FETCH</code> for use in
embedded SQL only. The variant of <code class="command">FETCH</code>
described here returns the data as if it were a
<code class="command">SELECT</code> result rather than placing it in host
variables. Other than this point, <code class="command">FETCH</code> is
fully upward-compatible with the SQL standard.
</p>
<p> The <code class="command">FETCH</code> forms involving
<code class="literal">FORWARD</code> and <code class="literal">BACKWARD</code>, as well
as the forms <code class="literal">FETCH <em class="replaceable"><code>count</code></em></code> and <code class="literal">FETCH
ALL</code>, in which <code class="literal">FORWARD</code> is implicit, are
<span class="productname">PostgreSQL</span> extensions.
</p>
<p> The SQL standard allows only <code class="literal">FROM</code> preceding the cursor
name; the option to use <code class="literal">IN</code> is an extension.
</p>
</div>
<div class="refsect1" lang="en">
<a name="id775722"></a><h2>See Also</h2>
<span class="simplelist"><a href="sql-close.html">CLOSE</a>, <a href="sql-declare.html">DECLARE</a>, <a href="sql-move.html">MOVE</a></span>
</div>
</div></body>
</html>
|