<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="declare-handler">
      </a>
      15.6.7.2 DECLARE ... HANDLER Statement
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045176040896">
  </a>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa32147597"><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> <em class="replaceable">handler_action</em> <span class="token keyword">HANDLER</span>
    <span class="token keyword">FOR</span> <em class="replaceable">condition_value</em> <span class="token punctuation">[</span><span class="token punctuation">,</span> <em class="replaceable">condition_value</em><span class="token punctuation">]</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    <em class="replaceable">statement</em>

<em class="replaceable">handler_action</em>: {
    <span class="token keyword">CONTINUE</span>
  <span class="token operator">|</span> <span class="token keyword">EXIT</span>
  <span class="token operator">|</span> <span class="token keyword">UNDO</span>
}

<em class="replaceable">condition_value</em>: {
    <em class="replaceable">mysql_error_code</em>
  <span class="token operator">|</span> <span class="token keyword">SQLSTATE</span> <span class="token punctuation">[</span><span class="token keyword">VALUE</span><span class="token punctuation">]</span> <em class="replaceable">sqlstate_value</em>
  <span class="token operator">|</span> <em class="replaceable">condition_name</em>
  <span class="token operator">|</span> <span class="token keyword">SQLWARNING</span>
  <span class="token operator">|</span> <span class="token operator">NOT</span> <span class="token keyword">FOUND</span>
  <span class="token operator">|</span> <span class="token keyword">SQLEXCEPTION</span>
}</code></pre>
  </div>
  <p>
   The
   <a class="link" href="declare-handler.html" title="15.6.7.2 DECLARE ... HANDLER Statement">
    <code class="literal">
     DECLARE ...
        HANDLER
    </code>
   </a>
   statement specifies a handler that deals with
        one or more conditions. If one of these conditions occurs, the
        specified
   <em class="replaceable">
    <code>
     statement
    </code>
   </em>
   executes.
   <em class="replaceable">
    <code>
     statement
    </code>
   </em>
   can be a simple statement
        such as
   <code class="literal">
    SET
    <em class="replaceable">
     <code>
      var_name
     </code>
    </em>
    =
    <em class="replaceable">
     <code>
      value
     </code>
    </em>
   </code>
   , or a compound
        statement written using
   <code class="literal">
    BEGIN
   </code>
   and
   <code class="literal">
    END
   </code>
   (see
   <a class="xref" href="begin-end.html" title="15.6.1 BEGIN ... END Compound Statement">
    Section 15.6.1, “BEGIN ... END Compound Statement”
   </a>
   ).
  </p>
  <p>
   Handler declarations must appear after variable or condition
        declarations.
  </p>
  <p>
   The
   <em class="replaceable">
    <code>
     handler_action
    </code>
   </em>
   value indicates
        what action the handler takes after execution of the handler
        statement:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <code class="literal">
       CONTINUE
      </code>
      : Execution of the current
            program continues.
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       EXIT
      </code>
      : Execution terminates for the
      <a class="link" href="begin-end.html" title="15.6.1 BEGIN ... END Compound Statement">
       <code class="literal">
        BEGIN ...
            END
       </code>
      </a>
      compound statement in which the handler is
            declared. This is true even if the condition occurs in an
            inner block.
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       UNDO
      </code>
      : Not supported.
     </p>
    </li>
   </ul>
  </div>
  <p>
   The
   <em class="replaceable">
    <code>
     condition_value
    </code>
   </em>
   for
   <a class="link" href="declare-handler.html" title="15.6.7.2 DECLARE ... HANDLER Statement">
    <code class="literal">
     DECLARE ...
        HANDLER
    </code>
   </a>
   indicates the specific condition or class of
        conditions that activates the handler. It can take the following
        forms:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <em class="replaceable">
       <code>
        mysql_error_code
       </code>
      </em>
      : An integer
            literal indicating a MySQL error code, such as 1051 to
            specify
      <span class="quote">
       “
       <span class="quote">
        unknown table
       </span>
       ”
      </span>
      :
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa78756762"><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">CONTINUE</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token number">1051</span>
  <span class="token keyword">BEGIN</span>
    <span class="token comment" spellcheck="true">-- body of handler</span>
  <span class="token keyword">END</span><span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      Do not use MySQL error code 0 because that indicates success
            rather than an error condition. For a list of MySQL error
            codes, see
      <a class="ulink" href="/doc/mysql-errors/8.4/en/server-error-reference.html" target="_top">
       Server Error Message Reference
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      SQLSTATE [VALUE]
      <em class="replaceable">
       <code>
        sqlstate_value
       </code>
      </em>
      :
            A 5-character string literal indicating an SQLSTATE value,
            such as
      <code class="literal">
       '42S01'
      </code>
      to specify
      <span class="quote">
       “
       <span class="quote">
        unknown
            table
       </span>
       ”
      </span>
      :
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa99446362"><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">CONTINUE</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token keyword">SQLSTATE</span> <span class="token string">'42S02'</span>
  <span class="token keyword">BEGIN</span>
    <span class="token comment" spellcheck="true">-- body of handler</span>
  <span class="token keyword">END</span><span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      Do not use SQLSTATE values that begin with
      <code class="literal">
       '00'
      </code>
      because those indicate success
            rather than an error condition. For a list of SQLSTATE
            values, see
      <a class="ulink" href="/doc/mysql-errors/8.4/en/server-error-reference.html" target="_top">
       Server Error Message Reference
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      <em class="replaceable">
       <code>
        condition_name
       </code>
      </em>
      : A condition name
            previously specified with
      <a class="link" href="declare-condition.html" title="15.6.7.1 DECLARE ... CONDITION Statement">
       <code class="literal">
        DECLARE
            ... CONDITION
       </code>
      </a>
      . A condition name can be associated
            with a MySQL error code or SQLSTATE value. See
      <a class="xref" href="declare-condition.html" title="15.6.7.1 DECLARE ... CONDITION Statement">
       Section 15.6.7.1, “DECLARE ... CONDITION Statement”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       SQLWARNING
      </code>
      : Shorthand for the class of
            SQLSTATE values that begin with
      <code class="literal">
       '01'
      </code>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa77822029"><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">CONTINUE</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token keyword">SQLWARNING</span>
  <span class="token keyword">BEGIN</span>
    <span class="token comment" spellcheck="true">-- body of handler</span>
  <span class="token keyword">END</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       NOT FOUND
      </code>
      : Shorthand for the class of
            SQLSTATE values that begin with
      <code class="literal">
       '02'
      </code>
      .
            This is relevant within the context of cursors and is used
            to control what happens when a cursor reaches the end of a
            data set. If no more rows are available, a No Data condition
            occurs with SQLSTATE value
      <code class="literal">
       '02000'
      </code>
      . To
            detect this condition, you can set up a handler for it or
            for a
      <code class="literal">
       NOT FOUND
      </code>
      condition.
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa44015137"><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">CONTINUE</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token operator">NOT</span> <span class="token keyword">FOUND</span>
  <span class="token keyword">BEGIN</span>
    <span class="token comment" spellcheck="true">-- body of handler</span>
  <span class="token keyword">END</span><span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      For another example, see
      <a class="xref" href="cursors.html" title="15.6.6 Cursors">
       Section 15.6.6, “Cursors”
      </a>
      . The
      <code class="literal">
       NOT FOUND
      </code>
      condition also occurs for
      <code class="literal">
       SELECT ... INTO
       <em class="replaceable">
        <code>
         var_list
        </code>
       </em>
      </code>
      statements
            that retrieve no rows.
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       SQLEXCEPTION
      </code>
      : Shorthand for the class of
            SQLSTATE values that do not begin with
      <code class="literal">
       '00'
      </code>
      ,
      <code class="literal">
       '01'
      </code>
      , or
      <code class="literal">
       '02'
      </code>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa32256674"><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">CONTINUE</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token keyword">SQLEXCEPTION</span>
  <span class="token keyword">BEGIN</span>
    <span class="token comment" spellcheck="true">-- body of handler</span>
  <span class="token keyword">END</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
   </ul>
  </div>
  <p>
   For information about how the server chooses handlers when a
        condition occurs, see
   <a class="xref" href="handler-scope.html" title="15.6.7.6 Scope Rules for Handlers">
    Section 15.6.7.6, “Scope Rules for Handlers”
   </a>
   .
  </p>
  <p>
   If a condition occurs for which no handler has been declared,
        the action taken depends on the condition class:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      For
      <code class="literal">
       SQLEXCEPTION
      </code>
      conditions, the stored
            program terminates at the statement that raised the
            condition, as if there were an
      <code class="literal">
       EXIT
      </code>
      handler. If the program was called by another stored
            program, the calling program handles the condition using the
            handler selection rules applied to its own handlers.
     </p>
    </li>
    <li class="listitem">
     <p>
      For
      <code class="literal">
       SQLWARNING
      </code>
      conditions, the program
            continues executing, as if there were a
      <code class="literal">
       CONTINUE
      </code>
      handler.
     </p>
    </li>
    <li class="listitem">
     <p>
      For
      <code class="literal">
       NOT FOUND
      </code>
      conditions, if the
            condition was raised normally, the action is
      <code class="literal">
       CONTINUE
      </code>
      . If it was raised by
      <a class="link" href="signal.html" title="15.6.7.5 SIGNAL Statement">
       <code class="literal">
        SIGNAL
       </code>
      </a>
      or
      <a class="link" href="resignal.html" title="15.6.7.4 RESIGNAL Statement">
       <code class="literal">
        RESIGNAL
       </code>
      </a>
      , the action is
      <code class="literal">
       EXIT
      </code>
      .
     </p>
    </li>
   </ul>
  </div>
  <p>
   The following example uses a handler for
   <code class="literal">
    SQLSTATE
        '23000'
   </code>
   , which occurs for a duplicate-key error:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa86090267"><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 prompt">mysql&gt;</span> <span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test<span class="token punctuation">.</span>t <span class="token punctuation">(</span>s1 <span class="token datatype">INT</span><span class="token punctuation">,</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token output">Query OK, 0 rows affected (0.00 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">delimiter</span> <span class="token comment" spellcheck="true">//</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">CREATE</span> <span class="token keyword">PROCEDURE</span> handlerdemo <span class="token punctuation">(</span><span class="token punctuation">)</span>
       <span class="token keyword">BEGIN</span>
         <span class="token keyword">DECLARE</span> <span class="token keyword">CONTINUE</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token keyword">SQLSTATE</span> <span class="token string">'23000'</span> <span class="token keyword">SET</span> <span class="token variable">@x2</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
         <span class="token keyword">SET</span> <span class="token variable">@x</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
         <span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test<span class="token punctuation">.</span>t <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">SET</span> <span class="token variable">@x</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
         <span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test<span class="token punctuation">.</span>t <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">SET</span> <span class="token variable">@x</span> <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
       <span class="token keyword">END</span><span class="token punctuation">;</span>
       <span class="token comment" spellcheck="true">//</span>
<span class="token output">Query OK, 0 rows affected (0.00 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">CALL</span> handlerdemo<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//</span>
<span class="token output">Query OK, 0 rows affected (0.00 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@x</span><span class="token comment" spellcheck="true">//</span>
<span class="token output">    <span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output">    <span class="token punctuation">|</span> @x   <span class="token punctuation">|</span></span>
<span class="token output">    <span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output">    <span class="token punctuation">|</span> 3    <span class="token punctuation">|</span></span>
<span class="token output">    <span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
    <span class="token number">1</span> <span class="token keyword">row</span> <span class="token keyword">in</span> <span class="token datatype">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span></code></pre>
  </div>
  <p>
   Notice that
   <code class="literal">
    @x
   </code>
   is
   <code class="literal">
    3
   </code>
   after
        the procedure executes, which shows that execution continued to
        the end of the procedure after the error occurred. If the
   <a class="link" href="declare-handler.html" title="15.6.7.2 DECLARE ... HANDLER Statement">
    <code class="literal">
     DECLARE ...
        HANDLER
    </code>
   </a>
   statement had not been present, MySQL would
        have taken the default action (
   <code class="literal">
    EXIT
   </code>
   ) after
        the second
   <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
    <code class="literal">
     INSERT
    </code>
   </a>
   failed due to
        the
   <code class="literal">
    PRIMARY KEY
   </code>
   constraint, and
   <code class="literal">
    SELECT @x
   </code>
   would have returned
   <code class="literal">
    2
   </code>
   .
  </p>
  <p>
   To ignore a condition, declare a
   <code class="literal">
    CONTINUE
   </code>
   handler for it and associate it with an empty block. For
        example:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa77692191"><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">CONTINUE</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token keyword">SQLWARNING</span> <span class="token keyword">BEGIN</span> <span class="token keyword">END</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   The scope of a block label does not include the code for
        handlers declared within the block. Therefore, the statement
        associated with a handler cannot use
   <a class="link" href="iterate.html" title="15.6.5.3 ITERATE Statement">
    <code class="literal">
     ITERATE
    </code>
   </a>
   or
   <a class="link" href="leave.html" title="15.6.5.4 LEAVE Statement">
    <code class="literal">
     LEAVE
    </code>
   </a>
   to refer to labels for
        blocks that enclose the handler declaration. Consider the
        following example, where the
   <a class="link" href="repeat.html" title="15.6.5.6 REPEAT Statement">
    <code class="literal">
     REPEAT
    </code>
   </a>
   block has a label of
   <code class="literal">
    retry
   </code>
   :
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa24358799"><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> p <span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">BEGIN</span>
  <span class="token keyword">DECLARE</span> i <span class="token datatype">INT</span> <span class="token keyword">DEFAULT</span> <span class="token number">3</span><span class="token punctuation">;</span>
  retry:
    <span class="token keyword">REPEAT</span>
      <span class="token keyword">BEGIN</span>
        <span class="token keyword">DECLARE</span> <span class="token keyword">CONTINUE</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token keyword">SQLWARNING</span>
          <span class="token keyword">BEGIN</span>
            <span class="token keyword">ITERATE</span> retry<span class="token punctuation">;</span>    <span class="token comment" spellcheck="true"># illegal</span>
          <span class="token keyword">END</span><span class="token punctuation">;</span>
        <span class="token keyword">IF</span> i <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token keyword">THEN</span>
          <span class="token keyword">LEAVE</span> retry<span class="token punctuation">;</span>        <span class="token comment" spellcheck="true"># legal</span>
        <span class="token keyword">END</span> <span class="token keyword">IF</span><span class="token punctuation">;</span>
        <span class="token keyword">SET</span> i <span class="token operator">=</span> i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
      <span class="token keyword">END</span><span class="token punctuation">;</span>
    <span class="token keyword">UNTIL</span> <span class="token boolean">FALSE</span> <span class="token keyword">END</span> <span class="token keyword">REPEAT</span><span class="token punctuation">;</span>
<span class="token keyword">END</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   The
   <code class="literal">
    retry
   </code>
   label is in scope for the
   <a class="link" href="if.html" title="15.6.5.2 IF Statement">
    <code class="literal">
     IF
    </code>
   </a>
   statement within the block. It
        is not in scope for the
   <code class="literal">
    CONTINUE
   </code>
   handler, so
        the reference there is invalid and results in an error:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-none"><div class="docs-select-all right" id="sa84965207"><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-none">ERROR 1308 (42000): LEAVE with no matching label: retry</code></pre>
  </div>
  <p>
   To avoid references to outer labels in handlers, use one of
        these strategies:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      To leave the block, use an
      <code class="literal">
       EXIT
      </code>
      handler.
            If no block cleanup is required, the
      <a class="link" href="begin-end.html" title="15.6.1 BEGIN ... END Compound Statement">
       <code class="literal">
        BEGIN ...
            END
       </code>
      </a>
      handler body can be empty:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa64915816"><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">EXIT</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token keyword">SQLWARNING</span> <span class="token keyword">BEGIN</span> <span class="token keyword">END</span><span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      Otherwise, put the cleanup statements in the handler body:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa35703363"><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">EXIT</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token keyword">SQLWARNING</span>
  <span class="token keyword">BEGIN</span>
    <span class="token keyword"></span><em class="replaceable"><span class="token keyword">block</span> cleanup statements</em>
  <span class="token keyword">END</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      To continue execution, set a status variable in a
      <code class="literal">
       CONTINUE
      </code>
      handler that can be checked in
            the enclosing block to determine whether the handler was
            invoked. The following example uses the variable
      <code class="literal">
       done
      </code>
      for this purpose:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa24963853"><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> p <span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">BEGIN</span>
  <span class="token keyword">DECLARE</span> i <span class="token datatype">INT</span> <span class="token keyword">DEFAULT</span> <span class="token number">3</span><span class="token punctuation">;</span>
  <span class="token keyword">DECLARE</span> done <span class="token datatype">INT</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">FALSE</span><span class="token punctuation">;</span>
  retry:
    <span class="token keyword">REPEAT</span>
      <span class="token keyword">BEGIN</span>
        <span class="token keyword">DECLARE</span> <span class="token keyword">CONTINUE</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token keyword">SQLWARNING</span>
          <span class="token keyword">BEGIN</span>
            <span class="token keyword">SET</span> done <span class="token operator">=</span> <span class="token boolean">TRUE</span><span class="token punctuation">;</span>
          <span class="token keyword">END</span><span class="token punctuation">;</span>
        <span class="token keyword">IF</span> done <span class="token operator">OR</span> i <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token keyword">THEN</span>
          <span class="token keyword">LEAVE</span> retry<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">SET</span> i <span class="token operator">=</span> i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
      <span class="token keyword">END</span><span class="token punctuation">;</span>
    <span class="token keyword">UNTIL</span> <span class="token boolean">FALSE</span> <span class="token keyword">END</span> <span class="token keyword">REPEAT</span><span class="token punctuation">;</span>
<span class="token keyword">END</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
   </ul>
  </div>
 </div>
 <br/>
</div>
