<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="declare-cursor">
      </a>
      15.6.6.2 Cursor DECLARE Statement
     </h4>
    </div>
   </div>
  </div>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa17583476"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">DECLARE</span> <span class="token keyword"><em class="replaceable">cursor_name</em></span> <span class="token keyword">CURSOR</span> <span class="token keyword">FOR</span> <em class="replaceable">select_statement</em></code></pre>
  </div>
  <p>
   This statement declares a cursor and associates it with a
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   statement that retrieves
        the rows to be traversed by the cursor. To fetch the rows later,
        use a
   <a class="link" href="fetch.html" title="15.6.6.3 Cursor FETCH Statement">
    <code class="literal">
     FETCH
    </code>
   </a>
   statement. The number
        of columns retrieved by the
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   statement must match the
        number of output variables specified in the
   <a class="link" href="fetch.html" title="15.6.6.3 Cursor FETCH Statement">
    <code class="literal">
     FETCH
    </code>
   </a>
   statement.
  </p>
  <p>
   The
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   statement cannot have
        an
   <code class="literal">
    INTO
   </code>
   clause.
  </p>
  <p>
   Cursor declarations must appear before handler declarations and
        after variable and condition declarations.
  </p>
  <p>
   A stored program may contain multiple cursor declarations, but
        each cursor declared in a given block must have a unique name.
        For an example, see
   <a class="xref" href="cursors.html" title="15.6.6 Cursors">
    Section 15.6.6, “Cursors”
   </a>
   .
  </p>
  <p>
   For information available through
   <a class="link" href="show.html" title="15.7.7 SHOW Statements">
    <code class="literal">
     SHOW
    </code>
   </a>
   statements, it is possible
        in many cases to obtain equivalent information by using a cursor
        with an
   <code class="literal">
    INFORMATION_SCHEMA
   </code>
   table.
  </p>
 </div>
 <br/>
</div>
