Home Explore Blog CI



postgresql

1st chunk of `doc/src/sgml/ref/fetch.sgml`
3ebd3f095621389675529490b1efdfdb1e2cd0d871827b3d0000000100000fa0
<!--
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 &mdash;
    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>

Title: FETCH: Retrieving Rows from a Cursor in PostgreSQL
Summary
The FETCH command retrieves rows from a query using a previously created cursor. It allows specifying the direction (NEXT, PRIOR, FIRST, LAST, ABSOLUTE, RELATIVE, FORWARD, BACKWARD) and number of rows to fetch. The cursor maintains a position that determines which rows are retrieved. Special cases like FETCH ALL, FETCH BACKWARD ALL, and fetching with a count of 0 are also explained. Note that cursor usage within PL/pgSQL functions follows different rules.