fetch 中文man頁面
NAME
FETCH - 用游標從查詢中抓取行
SYNOPSIS
FETCH [ direction { FROM | IN } ] cursorname where direction can be empty or one of: NEXT PRIOR FIRST LAST ABSOLUTE count RELATIVE count count ALL FORWARD FORWARD count FORWARD ALL BACKWARD BACKWARD count BACKWARD ALL
DESCRIPTION 描述
FETCH 使用游標檢索行。
一個游標有一個由 FETCH 使用的相關聯的位置。 游標得位置可以在查詢結果的***行之前,或者在結果中的任意行, 或者在結果的***一行之后。在創建完之后,游標是放在***行之前的。 在抓取了一些行之后,游標放在檢索到的***一行上。如果 FETCH 抓完了所有可用行,那么它就停在***一行后面,或者在向前抓去的情況下是停在***行前面。 FETCH ALL 或者 FETCH BACKWARD ALL 將總是把游標的位置放在***一行或者在***行前面。
NEXT, PRIOR, FIRST, LAST, ABSOLUTE, RELATIVE 形式在恰當地 移動游標之后抓取一個行。如果沒有數據行了,那么返回一個空的結果, 那么游標就會停在查詢結果的***一行之后或者在***行之前。
FORWARD 和 BACKWARD 形式在向前或者向后移動的過程中抓取指定的行數, 然后把游標定位在***返回的行上(或者是,如果 count 大于可用的行數,在所有行之前或之后。)
RELATIVE 0,FORWARD 0,和 BACKWARD 0 都要求在不移動游標的前提下抓取當前行---也就是重新抓取最近剛剛抓取過的行。 除非游標定位在***行之前或者***一行之后,這個動作都應該成功,而在那兩種情況下,不返回任何行。
PARAMETERS 參數
- direction
- direction 定義抓取的方向和抓取的行數。它可以是下述之一:
- NEXT
抓取下一行。 direction 省略時這是缺省值。- PRIOR
抓取前面一行。- FIRST
抓取查詢的***行(和 ABSOLUTE 1 相同)。- LAST
抓取查詢的***一行(和 ABSOLUTE -1 相同)。- ABSOLUTE count
抓取查詢中第 count 行, 或者,如果 count < 0, 從查詢結果末尾抓取第abs(count)行。 如果count 超出了范圍,那么定位在***行之前和***一行之后的位置; 特別是 ABSOLUTE 0 定位在***行之前。- RELATIVE count
- 抓取隨后的第 count 行, 或者,如果 count < 0 的時候, 抓取前面的第 abs(count) 行。 如果有數據的話,RELATIVE 0 重新抓取當前行。
- count
抓取下面的 count 行 (和 FORWARD count 一樣)。- ALL
抓取所有剩余的行(和 FORWARD ALL 一樣)。- FORWARD
抓取下面一行(和 NEXT)一樣。- FORWARD count
抓取下面 count 行。 FORWARD 0 重新抓取當前行。- FORWARD ALL
抓取所有剩余行。- BACKWARD
抓取前面一行(和 PRIOR 一樣)。- BACKWARD count
抓取前面 count 行(向后掃描)。 BACKWARD 0 重新抓取當前行。- BACKWARD ALL
抓取所有前面的行(向后掃描)。
- count
- count 可能是一個有符號的整數常量,決定要抓取的行數和方向。 對于 FORWARD 和 BACKWARD 的情況,聲明一個帶負號的 count 等效于改變 FORWARD 和 BACKWARD 的方向。
- cursorname
一個打開的游標的名稱。
OUTPUTS 輸出
成功完成時,一個 FETCH 命令返回一個形如下面的標記
FETCH count
這里的 count 是抓取的行數(可能是零)。 請注意在 psql 里,命令標簽實際上不會顯示, 因為 psql 用抓取的行數取代了。
NOTES 注意
如果你想使用 FETCH NEXT 之外的任何 FETCH 的變種, 或者是帶負數計數的 FETCH FORWARD。那么定義游標的時候應該帶著 SCROLL 選項。 對于簡單的查詢,PostgreSQL 會允許那些沒有帶 SCROLL 選項定義的游標也可以反向抓取, 但是我們***不要依賴這個行為。 如果游標定義了 NO SCROLL,那么不允許反向抓取。
ABSOLUTE 抓取不會比用相對位移移動到需要的數據行更快: 因為下層的實現必須遍歷所有中間的行。負數的絕對抓取甚至更糟糕: 查詢必須一直讀到結尾才能找到***一行,然后從那里開始反向遍歷。 不過,回退到查詢開頭(就像 FETCH ABSOLUTE 0)很快。
在游標中更新數據還不被 PostgreSQL 支持。
DECLARE [declare(7)] 語句用于定義一個游標。使用 MOVE [move(7)] 語句來改變游標位置而不檢索數據。
EXAMPLES 例子
下面的例子用一個游標跨過一個表。
BEGIN WORK; -- 建立一個游標: DECLARE liahona SCROLL CURSOR FOR SELECT * FROM films; -- 抓取頭 5 行到游標 liahona 里: 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 -- 抓取前面行: FETCH PRIOR FROM liahona; code | title | did | date_prod | kind | len -------+---------+-----+------------+--------+------- P_301 | Vertigo | 103 | 1958-11-14 | Action | 02:08 -- 關閉游標并提交事務: CLOSE liahona; COMMIT WORK;
COMPATIBILITY 兼容性
SQL 標準定義的 FETCH 只用于嵌入式環境下。 這里描述的 FETCH 變種是把結果數據像 SELECT 結果那樣返回,而不是把它放在宿主變量里。除了這點之外,FETCH 和 SQL 標準完全向上兼容。
涉及 FORWARD 和 BACKWARD 的 FETCH 形式 (包括 FETCH count 和 FETCH ALL 的形式,這個時候 FORWARD 是隱含的)是 PostgreSQL 的擴展。
SQL 標準只允許游標前面有 FROM, 用 IN 是一種擴展。
#p#
NAME
FETCH - retrieve rows from a query using a cursor
SYNOPSIS
FETCH [ direction { FROM | IN } ] cursorname where direction can be empty or one of: NEXT PRIOR FIRST LAST ABSOLUTE count RELATIVE count count ALL FORWARD FORWARD count FORWARD ALL BACKWARD BACKWARD count BACKWARD ALL
DESCRIPTION
FETCH retrieves rows using a previously-created cursor.
A cursor has an associated position, which is used by FETCH. 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 FETCH 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. FETCH ALL or FETCH BACKWARD ALL will always leave the cursor positioned after the last row or before the first row.
The forms NEXT, PRIOR, FIRST, LAST, ABSOLUTE, RELATIVE 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.
The forms using FORWARD and BACKWARD 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 count exceeds the number of rows available).
RELATIVE 0, FORWARD 0, and BACKWARD 0 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.
PARAMETERS
- direction
- direction defines the fetch direction and number of rows to fetch. It can be one of the following:
- NEXT
- Fetch the next row. This is the default if direction is omitted.
- PRIOR
- Fetch the prior row.
- FIRST
- Fetch the first row of the query (same as ABSOLUTE 1).
- LAST
- Fetch the last row of the query (same as ABSOLUTE -1).
- ABSOLUTE count
- Fetch the count'th row of the query, or the abs(count)'th row from the end if count is negative. Position before first row or after last row if count is out of range; in particular, ABSOLUTE 0 positions before the first row.
- RELATIVE count
- Fetch the count'th succeeding row, or the abs(count)'th prior row if count is negative. RELATIVE 0 re-fetches the current row, if any.
- count
- Fetch the next count rows (same as FORWARD count).
- ALL
- Fetch all remaining rows (same as FORWARD ALL).
- FORWARD
- Fetch the next row (same as NEXT).
- FORWARD count
- Fetch the next count rows. FORWARD 0 re-fetches the current row.
- FORWARD ALL
- Fetch all remaining rows.
- BACKWARD
- Fetch the prior row (same as PRIOR).
- BACKWARD count
- Fetch the prior count rows (scanning backwards). BACKWARD 0 re-fetches the current row.
- BACKWARD ALL
- Fetch all prior rows (scanning backwards).
- count
- count is a possibly-signed integer constant, determining the location or number of rows to fetch. For FORWARD and BACKWARD cases, specifying a negative count is equivalent to changing the sense of FORWARD and BACKWARD.
- cursorname
- An open cursor's name.
OUTPUTS
On successful completion, a FETCH command returns a command tag of the form
FETCH count
The count is the number of rows fetched (possibly zero). Note that in psql, the command tag will not actually be displayed, since psql displays the fetched rows instead.
NOTES
The cursor should be declared with the SCROLL option if one intends to use any variants of FETCH other than FETCH NEXT or FETCH FORWARD with a positive count. For simple queries PostgreSQL will allow backwards fetch from cursors not declared with SCROLL, but this behavior is best not relied on. If the cursor is declared with NO SCROLL, no backward fetches are allowed.
ABSOLUTE 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 FETCH ABSOLUTE 0) is fast.
Updating data via a cursor is currently not supported by PostgreSQL.
DECLARE [declare(7)] is used to define a cursor. Use MOVE [move(7)] to change cursor position without retrieving data.
EXAMPLES
The following example traverses a table using a cursor.
BEGIN WORK; -- Set up a cursor: DECLARE liahona SCROLL CURSOR FOR SELECT * FROM films; -- Fetch the first 5 rows in the cursor liahona: 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 -- Fetch the previous row: FETCH PRIOR FROM liahona; code | title | did | date_prod | kind | len -------+---------+-----+------------+--------+------- P_301 | Vertigo | 103 | 1958-11-14 | Action | 02:08 -- Close the cursor and end the transaction: CLOSE liahona; COMMIT WORK;
COMPATIBILITY
The SQL standard defines FETCH for use in embedded SQL only. This variant of FETCH described here returns the data as if it were a SELECT result rather than placing it in host variables. Other than this point, FETCH is fully upward-compatible with the SQL standard.
The FETCH forms involving FORWARD and BACKWARD, as well as the forms FETCH count and FETCH ALL, in which FORWARD is implicit, are PostgreSQL extensions.
The SQL standard allows only FROM preceding the cursor name; the option to use IN is an extension.