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.