Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/ref/fetch.sgml`
50a4576d923a871ed44903a697a70485a082150c0f7671020000000100000fa7
 <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>
       <varlistentry>
        <term><literal>NEXT</literal></term>
        <listitem>
         <para>
          Fetch the next row. This is the default if <replaceable
          class="parameter">direction</replaceable> is omitted.
         </para>
        </listitem>
       </varlistentry>

       <varlistentry>
        <term><literal>PRIOR</literal></term>
        <listitem>
         <para>
          Fetch the prior row.
         </para>
        </listitem>
       </varlistentry>

       <varlistentry>
        <term><literal>FIRST</literal></term>
        <listitem>
         <para>
          Fetch the first row of the query (same as <literal>ABSOLUTE 1</literal>).
         </para>
        </listitem>
       </varlistentry>

       <varlistentry>
        <term><literal>LAST</literal></term>
        <listitem>
         <para>
          Fetch the last row of the query (same as <literal>ABSOLUTE -1</literal>).
         </para>
        </listitem>
       </varlistentry>

       <varlistentry>
        <term><literal>ABSOLUTE <replaceable class="parameter">count</replaceable></literal></term>
        <listitem>
         <para>
          Fetch the <replaceable
          class="parameter">count</replaceable>'th row of the query,
          or the <literal>abs(<replaceable
          class="parameter">count</replaceable>)</literal>'th row from
          the end if <replaceable
          class="parameter">count</replaceable> is negative.  Position
          before first row or after last row if <replaceable
          class="parameter">count</replaceable> is out of range; in
          particular, <literal>ABSOLUTE 0</literal> positions before
          the first row.
         </para>
        </listitem>
       </varlistentry>

       <varlistentry>
        <term><literal>RELATIVE <replaceable class="parameter">count</replaceable></literal></term>
        <listitem>
         <para>
          Fetch the <replaceable
          class="parameter">count</replaceable>'th succeeding row, or
          the <literal>abs(<replaceable
          class="parameter">count</replaceable>)</literal>'th prior
          row if <replaceable class="parameter">count</replaceable> is
          negative.  <literal>RELATIVE 0</literal> re-fetches the
          current row, if any.
         </para>
        </listitem>
       </varlistentry>

       <varlistentry>
        <term><replaceable class="parameter">count</replaceable></term>
        <listitem>
         <para>
          Fetch the next <replaceable
          class="parameter">count</replaceable> rows (same as
          <literal>FORWARD <replaceable
          class="parameter">count</replaceable></literal>).
         </para>
        </listitem>
       </varlistentry>

       <varlistentry>
        <term><literal>ALL</literal></term>
        <listitem>
         <para>
          Fetch all remaining rows (same as <literal>FORWARD ALL</literal>).
         </para>
        </listitem>
       </varlistentry>

       <varlistentry>

Title: FETCH Parameters: Specifying Direction and Row Count
Summary
The FETCH command uses a 'direction' parameter to define how to fetch rows from a cursor. This includes options like NEXT (default), PRIOR, FIRST, LAST, ABSOLUTE (by row number), and RELATIVE (offset from current position). It also allows fetching multiple rows FORWARD or BACKWARD, or ALL remaining rows. Special cases like RELATIVE 0 are used to re-fetch the current row. Each direction option is explained in detail.