gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
15KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>FETCH</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-explain.html" title="EXPLAIN" /><link rel="next" href="sql-grant.html" title="GRANT" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">FETCH</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-explain.html" title="EXPLAIN">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-grant.html" title="GRANT">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-FETCH"><div class="titlepage"></div><a id="id-1.9.3.149.1" class="indexterm"></a><a id="id-1.9.3.149.2" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">FETCH</span></h2><p>FETCH — retrieve rows from a query using a cursor</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. FETCH [ <em class="replaceable"><code>direction</code></em> [ FROM | IN ] ] <em class="replaceable"><code>cursor_name</code></em>
  4. <span class="phrase">where <em class="replaceable"><code>direction</code></em> can be empty or one of:</span>
  5. NEXT
  6. PRIOR
  7. FIRST
  8. LAST
  9. ABSOLUTE <em class="replaceable"><code>count</code></em>
  10. RELATIVE <em class="replaceable"><code>count</code></em>
  11. <em class="replaceable"><code>count</code></em>
  12. ALL
  13. FORWARD
  14. FORWARD <em class="replaceable"><code>count</code></em>
  15. FORWARD ALL
  16. BACKWARD
  17. BACKWARD <em class="replaceable"><code>count</code></em>
  18. BACKWARD ALL
  19. </pre></div><div class="refsect1" id="id-1.9.3.149.6"><h2>Description</h2><p>
  20. <code class="command">FETCH</code> retrieves rows using a previously-created cursor.
  21. </p><p>
  22. A cursor has an associated position, which is used by
  23. <code class="command">FETCH</code>. The cursor position can be before the first row of the
  24. query result, on any particular row of the result, or after the last row
  25. of the result. When created, a cursor is positioned before the first row.
  26. After fetching some rows, the cursor is positioned on the row most recently
  27. retrieved. If <code class="command">FETCH</code> runs off the end of the available rows
  28. then the cursor is left positioned after the last row, or before the first
  29. row if fetching backward. <code class="command">FETCH ALL</code> or <code class="command">FETCH BACKWARD
  30. ALL</code> will always leave the cursor positioned after the last row or before
  31. the first row.
  32. </p><p>
  33. The forms <code class="literal">NEXT</code>, <code class="literal">PRIOR</code>, <code class="literal">FIRST</code>,
  34. <code class="literal">LAST</code>, <code class="literal">ABSOLUTE</code>, <code class="literal">RELATIVE</code> fetch
  35. a single row after moving the cursor appropriately. If there is no
  36. such row, an empty result is returned, and the cursor is left
  37. positioned before the first row or after the last row as
  38. appropriate.
  39. </p><p>
  40. The forms using <code class="literal">FORWARD</code> and <code class="literal">BACKWARD</code>
  41. retrieve the indicated number of rows moving in the forward or
  42. backward direction, leaving the cursor positioned on the
  43. last-returned row (or after/before all rows, if the <em class="replaceable"><code>count</code></em> exceeds the number of rows
  44. available).
  45. </p><p>
  46. <code class="literal">RELATIVE 0</code>, <code class="literal">FORWARD 0</code>, and
  47. <code class="literal">BACKWARD 0</code> all request fetching the current row without
  48. moving the cursor, that is, re-fetching the most recently fetched
  49. row. This will succeed unless the cursor is positioned before the
  50. first row or after the last row; in which case, no row is returned.
  51. </p><div class="note"><h3 class="title">Note</h3><p>
  52. This page describes usage of cursors at the SQL command level.
  53. If you are trying to use cursors inside a <span class="application">PL/pgSQL</span>
  54. function, the rules are different —
  55. see <a class="xref" href="plpgsql-cursors.html#PLPGSQL-CURSOR-USING" title="42.7.3. Using Cursors">Section 42.7.3</a>.
  56. </p></div></div><div class="refsect1" id="id-1.9.3.149.7"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>direction</code></em></span></dt><dd><p><em class="replaceable"><code>direction</code></em> defines
  57. the fetch direction and number of rows to fetch. It can be one
  58. of the following:
  59. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">NEXT</code></span></dt><dd><p>
  60. Fetch the next row. This is the default if <em class="replaceable"><code>direction</code></em> is omitted.
  61. </p></dd><dt><span class="term"><code class="literal">PRIOR</code></span></dt><dd><p>
  62. Fetch the prior row.
  63. </p></dd><dt><span class="term"><code class="literal">FIRST</code></span></dt><dd><p>
  64. Fetch the first row of the query (same as <code class="literal">ABSOLUTE 1</code>).
  65. </p></dd><dt><span class="term"><code class="literal">LAST</code></span></dt><dd><p>
  66. Fetch the last row of the query (same as <code class="literal">ABSOLUTE -1</code>).
  67. </p></dd><dt><span class="term"><code class="literal">ABSOLUTE <em class="replaceable"><code>count</code></em></code></span></dt><dd><p>
  68. Fetch the <em class="replaceable"><code>count</code></em>'th row of the query,
  69. or the <code class="literal">abs(<em class="replaceable"><code>count</code></em>)</code>'th row from
  70. the end if <em class="replaceable"><code>count</code></em> is negative. Position
  71. before first row or after last row if <em class="replaceable"><code>count</code></em> is out of range; in
  72. particular, <code class="literal">ABSOLUTE 0</code> positions before
  73. the first row.
  74. </p></dd><dt><span class="term"><code class="literal">RELATIVE <em class="replaceable"><code>count</code></em></code></span></dt><dd><p>
  75. Fetch the <em class="replaceable"><code>count</code></em>'th succeeding row, or
  76. the <code class="literal">abs(<em class="replaceable"><code>count</code></em>)</code>'th prior
  77. row if <em class="replaceable"><code>count</code></em> is
  78. negative. <code class="literal">RELATIVE 0</code> re-fetches the
  79. current row, if any.
  80. </p></dd><dt><span class="term"><em class="replaceable"><code>count</code></em></span></dt><dd><p>
  81. Fetch the next <em class="replaceable"><code>count</code></em> rows (same as
  82. <code class="literal">FORWARD <em class="replaceable"><code>count</code></em></code>).
  83. </p></dd><dt><span class="term"><code class="literal">ALL</code></span></dt><dd><p>
  84. Fetch all remaining rows (same as <code class="literal">FORWARD ALL</code>).
  85. </p></dd><dt><span class="term"><code class="literal">FORWARD</code></span></dt><dd><p>
  86. Fetch the next row (same as <code class="literal">NEXT</code>).
  87. </p></dd><dt><span class="term"><code class="literal">FORWARD <em class="replaceable"><code>count</code></em></code></span></dt><dd><p>
  88. Fetch the next <em class="replaceable"><code>count</code></em> rows.
  89. <code class="literal">FORWARD 0</code> re-fetches the current row.
  90. </p></dd><dt><span class="term"><code class="literal">FORWARD ALL</code></span></dt><dd><p>
  91. Fetch all remaining rows.
  92. </p></dd><dt><span class="term"><code class="literal">BACKWARD</code></span></dt><dd><p>
  93. Fetch the prior row (same as <code class="literal">PRIOR</code>).
  94. </p></dd><dt><span class="term"><code class="literal">BACKWARD <em class="replaceable"><code>count</code></em></code></span></dt><dd><p>
  95. Fetch the prior <em class="replaceable"><code>count</code></em> rows (scanning
  96. backwards). <code class="literal">BACKWARD 0</code> re-fetches the
  97. current row.
  98. </p></dd><dt><span class="term"><code class="literal">BACKWARD ALL</code></span></dt><dd><p>
  99. Fetch all prior rows (scanning backwards).
  100. </p></dd></dl></div></dd><dt><span class="term"><em class="replaceable"><code>count</code></em></span></dt><dd><p><em class="replaceable"><code>count</code></em> is a
  101. possibly-signed integer constant, determining the location or
  102. number of rows to fetch. For <code class="literal">FORWARD</code> and
  103. <code class="literal">BACKWARD</code> cases, specifying a negative <em class="replaceable"><code>count</code></em> is equivalent to changing
  104. the sense of <code class="literal">FORWARD</code> and <code class="literal">BACKWARD</code>.
  105. </p></dd><dt><span class="term"><em class="replaceable"><code>cursor_name</code></em></span></dt><dd><p>
  106. An open cursor's name.
  107. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.149.8"><h2>Outputs</h2><p>
  108. On successful completion, a <code class="command">FETCH</code> command returns a command
  109. tag of the form
  110. </p><pre class="screen">
  111. FETCH <em class="replaceable"><code>count</code></em>
  112. </pre><p>
  113. The <em class="replaceable"><code>count</code></em> is the number
  114. of rows fetched (possibly zero). Note that in
  115. <span class="application">psql</span>, the command tag will not actually be
  116. displayed, since <span class="application">psql</span> displays the fetched
  117. rows instead.
  118. </p></div><div class="refsect1" id="id-1.9.3.149.9"><h2>Notes</h2><p>
  119. The cursor should be declared with the <code class="literal">SCROLL</code>
  120. option if one intends to use any variants of <code class="command">FETCH</code>
  121. other than <code class="command">FETCH NEXT</code> or <code class="command">FETCH FORWARD</code> with
  122. a positive count. For simple queries
  123. <span class="productname">PostgreSQL</span> will allow backwards fetch
  124. from cursors not declared with <code class="literal">SCROLL</code>, but this
  125. behavior is best not relied on. If the cursor is declared with
  126. <code class="literal">NO SCROLL</code>, no backward fetches are allowed.
  127. </p><p>
  128. <code class="literal">ABSOLUTE</code> fetches are not any faster than
  129. navigating to the desired row with a relative move: the underlying
  130. implementation must traverse all the intermediate rows anyway.
  131. Negative absolute fetches are even worse: the query must be read to
  132. the end to find the last row, and then traversed backward from
  133. there. However, rewinding to the start of the query (as with
  134. <code class="literal">FETCH ABSOLUTE 0</code>) is fast.
  135. </p><p>
  136. <a class="xref" href="sql-declare.html" title="DECLARE"><span class="refentrytitle">DECLARE</span></a>
  137. is used to define a cursor. Use
  138. <a class="xref" href="sql-move.html" title="MOVE"><span class="refentrytitle">MOVE</span></a>
  139. to change cursor position without retrieving data.
  140. </p></div><div class="refsect1" id="id-1.9.3.149.10"><h2>Examples</h2><p>
  141. The following example traverses a table using a cursor:
  142. </p><pre class="programlisting">
  143. BEGIN WORK;
  144. -- Set up a cursor:
  145. DECLARE liahona SCROLL CURSOR FOR SELECT * FROM films;
  146. -- Fetch the first 5 rows in the cursor liahona:
  147. FETCH FORWARD 5 FROM liahona;
  148. code | title | did | date_prod | kind | len
  149. -------+-------------------------+-----+------------+----------+-------
  150. BL101 | The Third Man | 101 | 1949-12-23 | Drama | 01:44
  151. BL102 | The African Queen | 101 | 1951-08-11 | Romantic | 01:43
  152. JL201 | Une Femme est une Femme | 102 | 1961-03-12 | Romantic | 01:25
  153. P_301 | Vertigo | 103 | 1958-11-14 | Action | 02:08
  154. P_302 | Becket | 103 | 1964-02-03 | Drama | 02:28
  155. -- Fetch the previous row:
  156. FETCH PRIOR FROM liahona;
  157. code | title | did | date_prod | kind | len
  158. -------+---------+-----+------------+--------+-------
  159. P_301 | Vertigo | 103 | 1958-11-14 | Action | 02:08
  160. -- Close the cursor and end the transaction:
  161. CLOSE liahona;
  162. COMMIT WORK;
  163. </pre></div><div class="refsect1" id="id-1.9.3.149.11"><h2>Compatibility</h2><p>
  164. The SQL standard defines <code class="command">FETCH</code> for use in
  165. embedded SQL only. The variant of <code class="command">FETCH</code>
  166. described here returns the data as if it were a
  167. <code class="command">SELECT</code> result rather than placing it in host
  168. variables. Other than this point, <code class="command">FETCH</code> is
  169. fully upward-compatible with the SQL standard.
  170. </p><p>
  171. The <code class="command">FETCH</code> forms involving
  172. <code class="literal">FORWARD</code> and <code class="literal">BACKWARD</code>, as well
  173. as the forms <code class="literal">FETCH <em class="replaceable"><code>count</code></em></code> and <code class="literal">FETCH
  174. ALL</code>, in which <code class="literal">FORWARD</code> is implicit, are
  175. <span class="productname">PostgreSQL</span> extensions.
  176. </p><p>
  177. The SQL standard allows only <code class="literal">FROM</code> preceding the cursor
  178. name; the option to use <code class="literal">IN</code>, or to leave them out altogether, is
  179. an extension.
  180. </p></div><div class="refsect1" id="id-1.9.3.149.12"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-close.html" title="CLOSE"><span class="refentrytitle">CLOSE</span></a>, <a class="xref" href="sql-declare.html" title="DECLARE"><span class="refentrytitle">DECLARE</span></a>, <a class="xref" href="sql-move.html" title="MOVE"><span class="refentrytitle">MOVE</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-explain.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-grant.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">EXPLAIN </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> GRANT</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1