FETCH

Name

FETCH  --  Gets rows using a cursor カーソルを使用した行の入手。

Synopsis

FETCH [ selector ] [ count ] { IN | FROM } cursor
FETCH [ RELATIVE ] [ { [ # | ALL | NEXT | PRIOR ] } ] FROM ] cursor
  

入力

selector

selector defines the fetch direction. It can be one the following:

selector は行を 取り出す方向を定義します。次のうちの 1 つを使用できます。

FORWARD

fetch next row(s). This is the default if selector is omitted.

後方の(複数)行を取り出します。 これは、selector が省略された場合のデフォルト値です。

BACKWARD

fetch previous row(s).

前方の(複数)行を取り出します。

RELATIVE

Noise word for SQL92 compatibility.

SQL92 との互換性のためだけの余分な単語です。

count

count determines how many rows to fetch. It can be one of the following:

count は何行取り 出すのかを決定します。次のうちの 1 つを使用できます。

#

A signed integer that specify how many rows to fetch. Note that a negative integer is equivalent to changing the sense of FORWARD and BACKWARD.

取り出す行数の指定を行なう符合付き整数。負の整数値は FORWARD と BACKWARD の意味を切替えることと同じである ことに注意して下さい。

ALL

Retrieve all remaining rows.

残っている全ての行を取り出します。

NEXT

Equivalent to specifying a count of 1.

引数 count に 1 を指定することと 同じです。

PRIOR

Equivalent to specifying a count of -1.

引数 count に -1 を指定することと 同じです。

cursor

An open cursor's name.

開いているカーソルの名前。

出力

FETCH returns the results of the query defined by the specified cursor. The following messages will be returned if the query fails:

FETCH は指定カーソルによって定義された問い合わ せの結果を返します。問い合わせが失敗した場合には次のメッセージが 返されます。

NOTICE: PerformPortalFetch: portal "cursor" not found

If cursor is not previously declared. The cursor must be declared within a transaction block.

カーソルが事前に宣言されていない場合です。カーソルはトラ ンザクションブロック内で宣言されなければなりません。

NOTICE: FETCH/ABSOLUTE not supported, using RELATIVE

Postgres does not support absolute positioning of cursors.

Postgres はカーソルの絶対位置指定 をサポートしていません。

ERROR: FETCH/RELATIVE at current position is not supported

SQL92 allows one to repetatively retrieve the cursor at its "current position" using the syntax

SQL92 では

FETCH RELATIVE 0 FROM cursor
	
という構文を使ってカーソルの "現在位置" から繰り返し、データ を取り出すことができます。

Postgres does not currently support this notion; in fact the value zero is reserved to indicate that all rows should be retrieved and is equivalent to specifying the ALL keyword. If the RELATIVE keyword has been used, the Postgres assumes that the user intended SQL92 behavior and returns this error message.

Postgres では、現在この記述方法を サポートしていません。実際ゼロという値は全ての行を取り出さな ければならないことを示すものとして予約されており、ALL キーワ ードを指定したことと同じことになります。RELATIVE キーワードが 使用された場合、Postgres は、その ユーザが SQL92 の振舞いを意図したものとみ なし、エラーメッセージを返します。

説明

FETCH allows a user to retrieve rows using a cursor. The number of rows retrieved is specified by #. If the number of rows remaining in the cursor is less than #, then only those available are fetched. Substituting the keyword ALL in place of a number will cause all remaining rows in the cursor to be retrieved. Instances may be fetched in both FORWARD and BACKWARD directions. The default direction is FORWARD.

FETCH によって、ユーザはカーソルを使った行を取り出すことができま す。取り出される行数は # で指定されます。カ ーソルに残っている行数が # よりも少なかった場 合、残っているものだけが取り出されます。キーワード ALL を数の代わ りに指定すると、カーソルに残っている全ての行が取り出されます。 FORWARD(前方)にもBACKWARD(後方)にもインスタンスを取り出すこと ができます。デフォルトの方向は FORWARD です。

Tip: Negative numbers are now allowed to be specified for the row count. A negative number is equivalent to reversing the sense of the FORWARD and BACKWARD keywords. For example, FORWARD -1 is the same as BACKWARD 1.

負の値も行数の指定に使用することができます。負の値は FORWARD キーワードと BACKWARD キーワードの意味を逆にすることと同じで す。例えば、FORWARD -1BACKWARD 1 と同じになります。

Note that the FORWARD and BACKWARD keywords are Postgres extensions. The SQL92 syntax is also supported, specified in the second form of the command. See below for details on compatibility issues.

FORWARD と BACKWARD キーワードは Postgres の拡張であることに注意して 下さい。また、SQL92 構文も、このコマンドの 2 番目の形式として指定したものでサポートされています。詳細につ いては後述の互換性に関する論点を参照して下さい。

Once all rows are fetched, every other fetch access returns no rows.

全ての行がいったん取り出されると、その後の全ての取り出しアクセス は行を返しません。

Updating data in a cursor is not supported by Postgres, because mapping cursor updates back to base tables is not generally possible, as is also the case with VIEW updates. Consequently, users must issue explicit UPDATE commands to replace data.

通常、カーソルでの更新をどのように元にしたテーブルに戻すかを決 定することは、VIEW での更新の場合と同様にできませんので、カーソ ル内のデータの更新は Postgres でサポ ートされていません。データを入れ換えるには、明示的に UPDATE コ マンドを発行する必要があります。

Cursors may only be used inside of transactions because the data that they store spans multiple user queries.

カーソルが保存しているデータは複数のユーザの問い合わせでも使わ れますので、カーソルはトランザクションの内側でのみ使用できます。

注意

Use MOVE to change cursor position. DECLARE will define a cursor. Refer to BEGIN, COMMIT, and ROLLBACK for further information about transactions.

カーソルの位置を変更する場合は MOVE を使 用して下さい。DECLARE はカーソルを定義し ます。トランザクションに関するより詳細については BEGINCOMMITROLLBACK を参照して下さい。

使用法


   --set up and use a cursor:
   -- カーソルを設定し使用する。
   --
   BEGIN WORK;
     DECLARE liahona CURSOR
        FOR SELECT * FROM films;


   --Fetch first 5 rows in the cursor liahona:
   -- liahona カーソルから最初の5 行を取り出す。
   --
     FETCH FORWARD 5 IN 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 previous row:
   -- 直前の行を取り出す。
   --
     FETCH BACKWARD 1 IN liahona;

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


   -- close the cursor and commit work:
   -- カーソルを閉じ、作業をコミットする。
   --
     CLOSE liahona;
   COMMIT WORK;
   

互換性

The non-embedded use of cursors is a Postgres extension. The syntax and usage of cursors is being compared against the embedded form of cursors defined in SQL92.

非埋め込み型のカーソル使用は Postgres の 拡張です。このカーソルの構文と使用法は SQL92で定 義されている埋め込み形式と比較されてきました。

SQL92

SQL92 allows absolute positioning of the cursor for FETCH, and allows placing the results into explicit variables.

SQL92 では、FETCH にてカーソルの絶対位置を指定す ることができます。また、明示した変数に結果を代入することもできます。

FETCH ABSOLUTE #
    FROM cursor
    INTO :variable [, ...]
    

ABSOLUTE

The cursor should be positioned to the specified absolute row number. All row numbers in Postgres are relative numbers so this capability is not supported.

カーソルの位置は指定した絶対行番号に変更されなければなりませ ん。Postgres では全ての行番号は相 対的な番号ですので、この機能はサポートされません。

:variable

Target host variable(s).

対象となるホスト変数(複数可)。