バージョンごとのドキュメント一覧

FETCH

FETCH <refpurpose>retrieve rows from a query using a cursor</refpurpose> — カーソルを使用して問い合わせから行を取り出す

概要

Note the "direction" bit is also in ref/move.sgml
FETCH [ direction ] [ FROM | IN ] cursor_name


<phrase>where <replaceable class="parameter">direction</replaceable> can be one of:</phrase>

ここでdirectionには、次のいずれかを指定できます。

    NEXT
    PRIOR
    FIRST
    LAST
    ABSOLUTE count
    RELATIVE count
    count
    ALL
    FORWARD
    FORWARD count
    FORWARD ALL
    BACKWARD
    BACKWARD count
    BACKWARD ALL

説明

<title>Description</title>

<command>FETCH</command> retrieves rows using a previously-created cursor. FETCHは事前に作成されたカーソルを使用して行を取り出します。

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. カーソルはそれぞれ位置情報を持っており、FETCHはこれを使用します。 カーソルの位置は、問い合わせの結果の先頭行の前、結果内の任意の特定の行、結果の最終行の後のいずれにもなります。 カーソルの生成時は、カーソル位置は先頭行の前にあります。 行を取り出した後は、カーソル位置は最後に取り出した行にあります。 FETCHが利用可能な行の終わりを過ぎると、カーソル位置は最終行の後にあります(後方抽出の場合は先頭行の前になります)。 FETCH ALLもしくはFETCH BACKWARD ALLでは、カーソルの位置は常に最終行の後か、先頭行の前になります。

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. NEXTPRIORFIRSTLASTABSOLUTERELATIVE構文では、カーソルを適切に移動した後、行を1つ取り出します。 行が存在しない場合、空の結果が返され、カーソルは先頭行の前か最終行の後に適切に位置づけられます。

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). FORWARDおよびBACKWARDを使用した構文では、指定数の行を前方もしくは後方方向に取り出します。この時、最後に取り出した行がカーソル位置となります (countが利用可能な行数を超えた場合は、全行の後/前になります)。

<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. RELATIVE 0FORWARD 0、およびBACKWARD 0は全て、カーソルを移動することなく現在の行を取り出します。 つまり、一番最後に取り出した行を再度取り出すことになります。 カーソルが先頭行の前や最終行の後になければ、これらのコマンドは成功します。 先頭行の前や最終行の後にあれば、行は返されません。

注記

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"/>. このマニュアルページではSQLコマンドレベルでのカーソルの使用方法について説明しています。 PL/pgSQL内でカーソルを使用する場合は、規則が異なりますので、43.7.3を参照してください。

パラメータ

<title>Parameters</title>
direction
<para><replaceable class="parameter">direction</replaceable> defines the fetch direction and number of rows to fetch. It can be one of the following:

directionは、取り出す方向と取り出す行数を定義します。 以下のいずれかを指定できます。

NEXT

Fetch the next row. This is the default if <replaceable class="parameter">direction</replaceable> is omitted. 次の行を取り出します。 これは、directionが省略された時のデフォルトです。

PRIOR

Fetch the prior row. 1つ前の行を取り出します。

FIRST

Fetch the first row of the query (same as <literal>ABSOLUTE 1</literal>). 問い合わせの先頭行を取り出します (ABSOLUTE 1と同じです)。

LAST

Fetch the last row of the query (same as <literal>ABSOLUTE -1</literal>). 問い合わせの最終行を取り出します (ABSOLUTE -1と同じです)。

ABSOLUTE count

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. 問い合わせのcount番目の行を取り出します。 countが負ならば、終わりからabs(count)番目の行を取り出します。 countが範囲外の場合、カーソル位置は先頭行の前か最終行の後になります。 特に、ABSOLUTE 0と指定すると、先頭行の前になります。

RELATIVE count

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. カーソルの現在位置からcount番目の行を取り出します。 countが負の場合、現在位置よりabs(count)行分前の行を取り出します。 RELATIVE 0と指定すると、現在の行があれば、その行を再度取り出します。

count

Fetch the next <replaceable class="parameter">count</replaceable> rows (same as <literal>FORWARD <replaceable class="parameter">count</replaceable></literal>). 次のcount行を取り出します (FORWARD countと同じです)。

ALL

Fetch all remaining rows (same as <literal>FORWARD ALL</literal>). 残っている行を全て取り出します (FORWARD ALLと同じです)。

FORWARD

Fetch the next row (same as <literal>NEXT</literal>). 1つ次の行を取り出します (NEXTと同じです)。

FORWARD count

Fetch the next <replaceable class="parameter">count</replaceable> rows. <literal>FORWARD 0</literal> re-fetches the current row. 次のcount行分の行を取り出します。 FORWARD 0と指定すると、現在の行を再度取り出します。

FORWARD ALL

Fetch all remaining rows. 残っている行を全て取り出します。

BACKWARD

Fetch the prior row (same as <literal>PRIOR</literal>). 1つ前の行を取り出します (PRIORと同じです)。

BACKWARD count

Fetch the prior <replaceable class="parameter">count</replaceable> rows (scanning backwards). <literal>BACKWARD 0</literal> re-fetches the current row. 前のcount行分の行を(逆方向に走査して)取り出します。 BACKWARD 0と指定すると、現在の行を再度取り出します。

BACKWARD ALL

Fetch all prior rows (scanning backwards). 現在位置より前の行を(逆方向に走査して)全て取り出します。

count
<para><replaceable class="parameter">count</replaceable> is a possibly-signed integer constant, determining the location or number of rows to fetch. For <literal>FORWARD</literal> and <literal>BACKWARD</literal> cases, specifying a negative <replaceable class="parameter">count</replaceable> is equivalent to changing the sense of <literal>FORWARD</literal> and <literal>BACKWARD</literal>.

countは、整数定数で、符号を付けることができ、取り出す位置や行数を決定します。 FORWARDBACKWARDにおいて、countに負の値を指定するのは、FORWARDBACKWARDの意味を入れ替えるのと同等です。

cursor_name

An open cursor's name. 開いているカーソルの名前を指定します。

出力

<title>Outputs</title>

On successful completion, a <command>FETCH</command> command returns a command tag of the form 正常に終了すると、FETCHコマンドは以下の形式のコマンドタグを返します。

FETCH count

The <replaceable class="parameter">count</replaceable> is the number of rows fetched (possibly zero). Note that in <application>psql</application>, the command tag will not actually be displayed, since <application>psql</application> displays the fetched rows instead. countは取り出した行数です(0の可能性もあります)。 psqlでは、このコマンドタグは実際には表示されないことに注意してください。 psqlが、取り出した行を代わりに表示するためです。

注釈

<title>Notes</title>

The cursor should be declared with the <literal>SCROLL</literal> option if one intends to use any variants of <command>FETCH</command> other than <command>FETCH NEXT</command> or <command>FETCH FORWARD</command> with a positive count. For simple queries <productname>PostgreSQL</productname> will allow backwards fetch from cursors not declared with <literal>SCROLL</literal>, but this behavior is best not relied on. If the cursor is declared with <literal>NO SCROLL</literal>, no backward fetches are allowed. FETCHコマンドとして、FETCH NEXTもしくは正のcountのFETCH FORWARD以外を使用する場合、カーソルをSCROLLオプション付きで宣言しなければなりません。 単純な問い合わせでは、PostgreSQLでは、カーソルがSCROLL付きで宣言されていなくても後方向の取り出しを行うことができることがありますが、この動作に依存すべきではありません。 カーソルがNO SCROLL付きで宣言された場合は、後方向の取り出しを行うことができません。

<literal>ABSOLUTE</literal> 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 <literal>FETCH ABSOLUTE 0</literal>) is fast. ABSOLUTEによる取り出しは、相対的な指定による指定行への移動に比べて高速ではありません。 内部的な実装では、必ず中間の行を全て経由しているからです。 絶対指定で負の値を指定した場合、速度はさらに悪化します。 まず、最終行を見つけるために最後まで問い合わせを読み取って、その後に最終行から後方に移動するためです。 ただし、(FETCH ABSOLUTE 0を使用して)問い合わせの先頭へ戻るのは高速です。

<link linkend="sql-declare"><command>DECLARE</command></link> is used to define a cursor. Use <link linkend="sql-move"><command>MOVE</command></link> to change cursor position without retrieving data. DECLAREを使用してカーソルを定義します。 データを取り出さずにカーソル位置を変更する場合はMOVEを使用してください。

<title>Examples</title>

The following example traverses a table using a cursor: 次の例では、カーソルを使用してテーブル内を走査しています。

BEGIN WORK;


&#45;- Set up a cursor:

-- カーソルを設定します。
DECLARE liahona SCROLL CURSOR FOR SELECT * FROM films;


&#45;- Fetch the first 5 rows in the cursor liahona:

-- カーソルliahonaから最初の5行を取り出します。
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


&#45;- Fetch the previous row:

-- 1つ前の行を取り出します。
FETCH PRIOR FROM liahona;

 code  |  title  | did | date_prod  |  kind  |  len
-------+---------+-----+------------+--------+-------
 P_301 | Vertigo | 103 | 1958-11-14 | Action | 02:08


&#45;- Close the cursor and end the transaction:

-- カーソルを閉じ、トランザクションを終了します。
CLOSE liahona;
COMMIT WORK;

互換性

<title>Compatibility</title>

The SQL standard defines <command>FETCH</command> for use in embedded SQL only. The variant of <command>FETCH</command> described here returns the data as if it were a <command>SELECT</command> result rather than placing it in host variables. Other than this point, <command>FETCH</command> is fully upward-compatible with the SQL standard. 標準SQLでは、埋め込みSQLにおけるFETCHのみが定義されています。 上記で説明した各種のFETCHは、ホスト変数への代入ではなく、SELECTの結果であるかのようにデータを返します。 この点を除き、FETCHは完全に標準SQLと上位互換性を持ちます。

The <command>FETCH</command> forms involving <literal>FORWARD</literal> and <literal>BACKWARD</literal>, as well as the forms <literal>FETCH <replaceable class="parameter">count</replaceable></literal> and <literal>FETCH ALL</literal>, in which <literal>FORWARD</literal> is implicit, are <productname>PostgreSQL</productname> extensions. FORWARDBACKWARDを持つFETCHの形式や、暗黙的なFORWARDを持つFETCH countFETCH ALLPostgreSQLの拡張です。

The SQL standard allows only <literal>FROM</literal> preceding the cursor name; the option to use <literal>IN</literal>, or to leave them out altogether, is an extension. 標準SQLでは、カーソル名の前に付けられるのはFROMのみです。 INを使用するオプション、または、どちらも省略することはPostgreSQLの拡張です。

関連項目

<title>See Also</title> CLOSE, DECLARE, MOVE