<!--
doc/src/sgml/ref/fetch.sgml
PostgreSQL documentation
-->
<refentry id="sql-fetch">
<indexterm zone="sql-fetch">
<primary>FETCH</primary>
</indexterm>
<indexterm zone="sql-fetch">
<primary>cursor</primary>
<secondary>FETCH</secondary>
</indexterm>
<refmeta>
<refentrytitle>FETCH</refentrytitle>
<manvolnum>7</manvolnum>
<refmiscinfo>SQL - Language Statements</refmiscinfo>
</refmeta>
<refnamediv>
<refname>FETCH</refname>
<refpurpose>retrieve rows from a query using a cursor</refpurpose>
</refnamediv>
<refsynopsisdiv>
<!-- Note the "direction" bit is also in ref/move.sgml -->
<synopsis>
FETCH [ <replaceable class="parameter">direction</replaceable> ] [ FROM | IN ] <replaceable class="parameter">cursor_name</replaceable>
<phrase>where <replaceable class="parameter">direction</replaceable> can be one of:</phrase>
NEXT
PRIOR
FIRST
LAST
ABSOLUTE <replaceable class="parameter">count</replaceable>
RELATIVE <replaceable class="parameter">count</replaceable>
<replaceable class="parameter">count</replaceable>
ALL
FORWARD
FORWARD <replaceable class="parameter">count</replaceable>
FORWARD ALL
BACKWARD
BACKWARD <replaceable class="parameter">count</replaceable>
BACKWARD ALL
</synopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para>
<command>FETCH</command> retrieves rows using a previously-created cursor.
</para>
<para>
A cursor has an associated position, which is used by
<command>FETCH</command>. 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 <command>FETCH</command> 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. <command>FETCH ALL</command> or <command>FETCH BACKWARD
ALL</command> will always leave the cursor positioned after the last row or before
the first row.
</para>
<para>
The forms <literal>NEXT</literal>, <literal>PRIOR</literal>, <literal>FIRST</literal>,
<literal>LAST</literal>, <literal>ABSOLUTE</literal>, <literal>RELATIVE</literal> 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.
</para>
<para>
The forms using <literal>FORWARD</literal> and <literal>BACKWARD</literal>
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 <replaceable
class="parameter">count</replaceable> exceeds the number of rows
available).
</para>
<para>
<literal>RELATIVE 0</literal>, <literal>FORWARD 0</literal>, and
<literal>BACKWARD 0</literal> 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.
</para>
<note>
<para>
This page describes usage of cursors at the SQL command level.
If you are trying to use cursors inside a <application>PL/pgSQL</application>
function, the rules are different —
see <xref linkend="plpgsql-cursor-using"/>.
</para>
</note>
</refsect1>
<refsect1>
<title>Parameters</title>
<variablelist>
<varlistentry>
<term><replaceable class="parameter">direction</replaceable></term>
<listitem>
<para><replaceable class="parameter">direction</replaceable> defines
the fetch direction and number of rows to fetch. It can be one
of the following:
<variablelist>