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.

191 line
16KB

  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>DECLARE</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-deallocate.html" title="DEALLOCATE" /><link rel="next" href="sql-delete.html" title="DELETE" /></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">DECLARE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-deallocate.html" title="DEALLOCATE">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-delete.html" title="DELETE">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-DECLARE"><div class="titlepage"></div><a id="id-1.9.3.99.1" class="indexterm"></a><a id="id-1.9.3.99.2" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">DECLARE</span></h2><p>DECLARE — define a cursor</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. DECLARE <em class="replaceable"><code>name</code></em> [ BINARY ] [ INSENSITIVE ] [ [ NO ] SCROLL ]
  4. CURSOR [ { WITH | WITHOUT } HOLD ] FOR <em class="replaceable"><code>query</code></em>
  5. </pre></div><div class="refsect1" id="id-1.9.3.99.6"><h2>Description</h2><p>
  6. <code class="command">DECLARE</code> allows a user to create cursors, which
  7. can be used to retrieve
  8. a small number of rows at a time out of a larger query.
  9. After the cursor is created, rows are fetched from it using
  10. <a class="xref" href="sql-fetch.html" title="FETCH"><span class="refentrytitle">FETCH</span></a>.
  11. </p><div class="note"><h3 class="title">Note</h3><p>
  12. This page describes usage of cursors at the SQL command level.
  13. If you are trying to use cursors inside a <span class="application">PL/pgSQL</span>
  14. function, the rules are different —
  15. see <a class="xref" href="plpgsql-cursors.html" title="42.7. Cursors">Section 42.7</a>.
  16. </p></div></div><div class="refsect1" id="id-1.9.3.99.7"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  17. The name of the cursor to be created.
  18. </p></dd><dt><span class="term"><code class="literal">BINARY</code></span></dt><dd><p>
  19. Causes the cursor to return data in binary rather than in text format.
  20. </p></dd><dt><span class="term"><code class="literal">INSENSITIVE</code></span></dt><dd><p>
  21. Indicates that data retrieved from the cursor should be
  22. unaffected by updates to the table(s) underlying the cursor that occur
  23. after the cursor is created. In <span class="productname">PostgreSQL</span>,
  24. this is the default behavior; so this key word has no
  25. effect and is only accepted for compatibility with the SQL standard.
  26. </p></dd><dt><span class="term"><code class="literal">SCROLL</code><br /></span><span class="term"><code class="literal">NO SCROLL</code></span></dt><dd><p><code class="literal">SCROLL</code> specifies that the cursor can be used
  27. to retrieve rows in a nonsequential fashion (e.g.,
  28. backward). Depending upon the complexity of the query's
  29. execution plan, specifying <code class="literal">SCROLL</code> might impose
  30. a performance penalty on the query's execution time.
  31. <code class="literal">NO SCROLL</code> specifies that the cursor cannot be
  32. used to retrieve rows in a nonsequential fashion. The default is to
  33. allow scrolling in some cases; this is not the same as specifying
  34. <code class="literal">SCROLL</code>. See <a class="xref" href="sql-declare.html#SQL-DECLARE-NOTES" title="Notes">Notes</a> for details.
  35. </p></dd><dt><span class="term"><code class="literal">WITH HOLD</code><br /></span><span class="term"><code class="literal">WITHOUT HOLD</code></span></dt><dd><p><code class="literal">WITH HOLD</code> specifies that the cursor can
  36. continue to be used after the transaction that created it
  37. successfully commits. <code class="literal">WITHOUT HOLD</code> specifies
  38. that the cursor cannot be used outside of the transaction that
  39. created it. If neither <code class="literal">WITHOUT HOLD</code> nor
  40. <code class="literal">WITH HOLD</code> is specified, <code class="literal">WITHOUT
  41. HOLD</code> is the default.
  42. </p></dd><dt><span class="term"><em class="replaceable"><code>query</code></em></span></dt><dd><p>
  43. A <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a> or
  44. <a class="xref" href="sql-values.html" title="VALUES"><span class="refentrytitle">VALUES</span></a> command
  45. which will provide the rows to be returned by the cursor.
  46. </p></dd></dl></div><p>
  47. The key words <code class="literal">BINARY</code>,
  48. <code class="literal">INSENSITIVE</code>, and <code class="literal">SCROLL</code> can
  49. appear in any order.
  50. </p></div><div class="refsect1" id="SQL-DECLARE-NOTES"><h2>Notes</h2><p>
  51. Normal cursors return data in text format, the same as a
  52. <code class="command">SELECT</code> would produce. The <code class="literal">BINARY</code> option
  53. specifies that the cursor should return data in binary format.
  54. This reduces conversion effort for both the server and client,
  55. at the cost of more programmer effort to deal with platform-dependent
  56. binary data formats.
  57. As an example, if a query returns a value of one from an integer column,
  58. you would get a string of <code class="literal">1</code> with a default cursor,
  59. whereas with a binary cursor you would get
  60. a 4-byte field containing the internal representation of the value
  61. (in big-endian byte order).
  62. </p><p>
  63. Binary cursors should be used carefully. Many applications,
  64. including <span class="application">psql</span>, are not prepared to
  65. handle binary cursors and expect data to come back in the text
  66. format.
  67. </p><div class="note"><h3 class="title">Note</h3><p>
  68. When the client application uses the <span class="quote">“<span class="quote">extended query</span>”</span> protocol
  69. to issue a <code class="command">FETCH</code> command, the Bind protocol message
  70. specifies whether data is to be retrieved in text or binary format.
  71. This choice overrides the way that the cursor is defined. The concept
  72. of a binary cursor as such is thus obsolete when using extended query
  73. protocol — any cursor can be treated as either text or binary.
  74. </p></div><p>
  75. Unless <code class="literal">WITH HOLD</code> is specified, the cursor
  76. created by this command can only be used within the current
  77. transaction. Thus, <code class="command">DECLARE</code> without <code class="literal">WITH
  78. HOLD</code> is useless outside a transaction block: the cursor would
  79. survive only to the completion of the statement. Therefore
  80. <span class="productname">PostgreSQL</span> reports an error if such a
  81. command is used outside a transaction block.
  82. Use
  83. <a class="xref" href="sql-begin.html" title="BEGIN"><span class="refentrytitle">BEGIN</span></a> and
  84. <a class="xref" href="sql-commit.html" title="COMMIT"><span class="refentrytitle">COMMIT</span></a>
  85. (or <a class="xref" href="sql-rollback.html" title="ROLLBACK"><span class="refentrytitle">ROLLBACK</span></a>)
  86. to define a transaction block.
  87. </p><p>
  88. If <code class="literal">WITH HOLD</code> is specified and the transaction
  89. that created the cursor successfully commits, the cursor can
  90. continue to be accessed by subsequent transactions in the same
  91. session. (But if the creating transaction is aborted, the cursor
  92. is removed.) A cursor created with <code class="literal">WITH HOLD</code>
  93. is closed when an explicit <code class="command">CLOSE</code> command is
  94. issued on it, or the session ends. In the current implementation,
  95. the rows represented by a held cursor are copied into a temporary
  96. file or memory area so that they remain available for subsequent
  97. transactions.
  98. </p><p>
  99. <code class="literal">WITH HOLD</code> may not be specified when the query
  100. includes <code class="literal">FOR UPDATE</code> or <code class="literal">FOR SHARE</code>.
  101. </p><p>
  102. The <code class="literal">SCROLL</code> option should be specified when defining a
  103. cursor that will be used to fetch backwards. This is required by
  104. the SQL standard. However, for compatibility with earlier
  105. versions, <span class="productname">PostgreSQL</span> will allow
  106. backward fetches without <code class="literal">SCROLL</code>, if the cursor's query
  107. plan is simple enough that no extra overhead is needed to support
  108. it. However, application developers are advised not to rely on
  109. using backward fetches from a cursor that has not been created
  110. with <code class="literal">SCROLL</code>. If <code class="literal">NO SCROLL</code> is
  111. specified, then backward fetches are disallowed in any case.
  112. </p><p>
  113. Backward fetches are also disallowed when the query
  114. includes <code class="literal">FOR UPDATE</code> or <code class="literal">FOR SHARE</code>; therefore
  115. <code class="literal">SCROLL</code> may not be specified in this case.
  116. </p><div class="caution"><h3 class="title">Caution</h3><p>
  117. Scrollable and <code class="literal">WITH HOLD</code> cursors may give unexpected
  118. results if they invoke any volatile functions (see <a class="xref" href="xfunc-volatility.html" title="37.7. Function Volatility Categories">Section 37.7</a>). When a previously fetched row is
  119. re-fetched, the functions might be re-executed, perhaps leading to
  120. results different from the first time. One workaround for such cases
  121. is to declare the cursor <code class="literal">WITH HOLD</code> and commit the
  122. transaction before reading any rows from it. This will force the
  123. entire output of the cursor to be materialized in temporary storage,
  124. so that volatile functions are executed exactly once for each row.
  125. </p></div><p>
  126. If the cursor's query includes <code class="literal">FOR UPDATE</code> or <code class="literal">FOR
  127. SHARE</code>, then returned rows are locked at the time they are first
  128. fetched, in the same way as for a regular
  129. <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a> command with
  130. these options.
  131. In addition, the returned rows will be the most up-to-date versions;
  132. therefore these options provide the equivalent of what the SQL standard
  133. calls a <span class="quote">“<span class="quote">sensitive cursor</span>”</span>. (Specifying <code class="literal">INSENSITIVE</code>
  134. together with <code class="literal">FOR UPDATE</code> or <code class="literal">FOR SHARE</code> is an error.)
  135. </p><div class="caution"><h3 class="title">Caution</h3><p>
  136. It is generally recommended to use <code class="literal">FOR UPDATE</code> if the cursor
  137. is intended to be used with <code class="command">UPDATE ... WHERE CURRENT OF</code> or
  138. <code class="command">DELETE ... WHERE CURRENT OF</code>. Using <code class="literal">FOR UPDATE</code>
  139. prevents other sessions from changing the rows between the time they are
  140. fetched and the time they are updated. Without <code class="literal">FOR UPDATE</code>,
  141. a subsequent <code class="literal">WHERE CURRENT OF</code> command will have no effect if
  142. the row was changed since the cursor was created.
  143. </p><p>
  144. Another reason to use <code class="literal">FOR UPDATE</code> is that without it, a
  145. subsequent <code class="literal">WHERE CURRENT OF</code> might fail if the cursor query
  146. does not meet the SQL standard's rules for being <span class="quote">“<span class="quote">simply
  147. updatable</span>”</span> (in particular, the cursor must reference just one table
  148. and not use grouping or <code class="literal">ORDER BY</code>). Cursors
  149. that are not simply updatable might work, or might not, depending on plan
  150. choice details; so in the worst case, an application might work in testing
  151. and then fail in production. If <code class="literal">FOR UPDATE</code> is
  152. specified, the cursor is guaranteed to be updatable.
  153. </p><p>
  154. The main reason not to use <code class="literal">FOR UPDATE</code> with <code class="literal">WHERE
  155. CURRENT OF</code> is if you need the cursor to be scrollable, or to be
  156. insensitive to the subsequent updates (that is, continue to show the old
  157. data). If this is a requirement, pay close heed to the caveats shown
  158. above.
  159. </p></div><p>
  160. The SQL standard only makes provisions for cursors in embedded
  161. <acronym class="acronym">SQL</acronym>. The <span class="productname">PostgreSQL</span>
  162. server does not implement an <code class="command">OPEN</code> statement for
  163. cursors; a cursor is considered to be open when it is declared.
  164. However, <span class="application">ECPG</span>, the embedded SQL
  165. preprocessor for <span class="productname">PostgreSQL</span>, supports
  166. the standard SQL cursor conventions, including those involving
  167. <code class="command">DECLARE</code> and <code class="command">OPEN</code> statements.
  168. </p><p>
  169. You can see all available cursors by querying the <a class="link" href="view-pg-cursors.html" title="51.69. pg_cursors"><code class="structname">pg_cursors</code></a>
  170. system view.
  171. </p></div><div class="refsect1" id="id-1.9.3.99.9"><h2>Examples</h2><p>
  172. To declare a cursor:
  173. </p><pre class="programlisting">
  174. DECLARE liahona CURSOR FOR SELECT * FROM films;
  175. </pre><p>
  176. See <a class="xref" href="sql-fetch.html" title="FETCH"><span class="refentrytitle">FETCH</span></a> for more
  177. examples of cursor usage.
  178. </p></div><div class="refsect1" id="id-1.9.3.99.10"><h2>Compatibility</h2><p>
  179. The SQL standard says that it is implementation-dependent whether cursors
  180. are sensitive to concurrent updates of the underlying data by default. In
  181. <span class="productname">PostgreSQL</span>, cursors are insensitive by default,
  182. and can be made sensitive by specifying <code class="literal">FOR UPDATE</code>. Other
  183. products may work differently.
  184. </p><p>
  185. The SQL standard allows cursors only in embedded
  186. <acronym class="acronym">SQL</acronym> and in modules. <span class="productname">PostgreSQL</span>
  187. permits cursors to be used interactively.
  188. </p><p>
  189. Binary cursors are a <span class="productname">PostgreSQL</span>
  190. extension.
  191. </p></div><div class="refsect1" id="id-1.9.3.99.11"><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-fetch.html" title="FETCH"><span class="refentrytitle">FETCH</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-deallocate.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-delete.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">DEALLOCATE </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> DELETE</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1