<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="loop">
      </a>
      15.6.5.5 LOOP Statement
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045176242880">
  </a>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa39865881"><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 punctuation">[</span><em class="replaceable">begin_label</em>:<span class="token punctuation">]</span> <span class="token keyword">LOOP</span>
    <em class="replaceable">statement_list</em>
<span class="token keyword">END</span> <span class="token keyword">LOOP</span> <span class="token punctuation">[</span><em class="replaceable">end_label</em><span class="token punctuation">]</span></code></pre>
  </div>
  <p>
   <a class="link" href="loop.html" title="15.6.5.5 LOOP Statement">
    <code class="literal">
     LOOP
    </code>
   </a>
   implements a simple loop
        construct, enabling repeated execution of the statement list,
        which consists of one or more statements, each terminated by a
        semicolon (
   <code class="literal">
    ;
   </code>
   ) statement delimiter. The
        statements within the loop are repeated until the loop is
        terminated. Usually, this is accomplished with a
   <a class="link" href="leave.html" title="15.6.5.4 LEAVE Statement">
    <code class="literal">
     LEAVE
    </code>
   </a>
   statement. Within a stored
        function,
   <a class="link" href="return.html" title="15.6.5.7 RETURN Statement">
    <code class="literal">
     RETURN
    </code>
   </a>
   can also be
        used, which exits the function entirely.
  </p>
  <p>
   Neglecting to include a loop-termination statement results in an
        infinite loop.
  </p>
  <p>
   A
   <a class="link" href="loop.html" title="15.6.5.5 LOOP Statement">
    <code class="literal">
     LOOP
    </code>
   </a>
   statement can be labeled.
        For the rules regarding label use, see
   <a class="xref" href="statement-labels.html" title="15.6.2 Statement Labels">
    Section 15.6.2, “Statement Labels”
   </a>
   .
  </p>
  <p>
   Example:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa44847399"><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">CREATE</span> <span class="token keyword">PROCEDURE</span> doiterate<span class="token punctuation">(</span>p1 <span class="token datatype">INT</span><span class="token punctuation">)</span>
<span class="token keyword">BEGIN</span>
  label1: <span class="token keyword">LOOP</span>
    <span class="token keyword">SET</span> p1 <span class="token operator">=</span> p1 <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">IF</span> p1 <span class="token operator">&lt;</span> <span class="token number">10</span> <span class="token keyword">THEN</span>
      <span class="token keyword">ITERATE</span> label1<span class="token punctuation">;</span>
    <span class="token keyword">END</span> <span class="token keyword">IF</span><span class="token punctuation">;</span>
    <span class="token keyword">LEAVE</span> label1<span class="token punctuation">;</span>
  <span class="token keyword">END</span> <span class="token keyword">LOOP</span> label1<span class="token punctuation">;</span>
  <span class="token keyword">SET</span> <span class="token variable">@x</span> <span class="token operator">=</span> p1<span class="token punctuation">;</span>
<span class="token keyword">END</span><span class="token punctuation">;</span></code></pre>
  </div>
 </div>
 <br/>
</div>
