<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="declare-condition">
      </a>
      15.6.7.1 DECLARE ... CONDITION Statement
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045176070592">
  </a>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa89639331"><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">condition_name</em> <span class="token keyword">CONDITION</span> <span class="token keyword">FOR</span> <em class="replaceable">condition_value</em>

<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>
}</code></pre>
  </div>
  <p>
   The
   <a class="link" href="declare-condition.html" title="15.6.7.1 DECLARE ... CONDITION Statement">
    <code class="literal">
     DECLARE
        ... CONDITION
    </code>
   </a>
   statement declares a named error
        condition, associating a name with a condition that needs
        specific handling. The name can be referred to in a subsequent
   <a class="link" href="declare-handler.html" title="15.6.7.2 DECLARE ... HANDLER Statement">
    <code class="literal">
     DECLARE ...
        HANDLER
    </code>
   </a>
   statement (see
   <a class="xref" href="declare-handler.html" title="15.6.7.2 DECLARE ... HANDLER Statement">
    Section 15.6.7.2, “DECLARE ... HANDLER Statement”
   </a>
   ).
  </p>
  <p>
   Condition declarations must appear before cursor or handler
        declarations.
  </p>
  <p>
   The
   <em class="replaceable">
    <code>
     condition_value
    </code>
   </em>
   for
   <a class="link" href="declare-condition.html" title="15.6.7.1 DECLARE ... CONDITION Statement">
    <code class="literal">
     DECLARE ...
        CONDITION
    </code>
   </a>
   indicates the specific condition or class of
        conditions to associate with the condition name. 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.
     </p>
     <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.
     </p>
     <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>
   </ul>
  </div>
  <p>
   Condition names referred to in
   <a class="link" href="signal.html" title="15.6.7.5 SIGNAL Statement">
    <code class="literal">
     SIGNAL
    </code>
   </a>
   or use
   <a class="link" href="resignal.html" title="15.6.7.4 RESIGNAL Statement">
    <code class="literal">
     RESIGNAL
    </code>
   </a>
   statements must be
        associated with SQLSTATE values, not MySQL error codes.
  </p>
  <p>
   Using names for conditions can help make stored program code
        clearer. For example, this handler applies to attempts to drop a
        nonexistent table, but that is apparent only if you know that
        1051 is the MySQL error code for
   <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="sa36058060"><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>
   By declaring a name for the condition, the purpose of the
        handler is more readily seen:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa93727808"><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> no_such_table <span class="token keyword">CONDITION</span> <span class="token keyword">FOR</span> <span class="token number">1051</span><span class="token punctuation">;</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> no_such_table
  <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>
   Here is a named condition for the same condition, but based on
        the corresponding SQLSTATE value rather than the MySQL error
        code:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa63951923"><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> no_such_table <span class="token keyword">CONDITION</span> <span class="token keyword">FOR</span> <span class="token keyword">SQLSTATE</span> <span class="token string">'42S02'</span><span class="token punctuation">;</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> no_such_table
  <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>
 </div>
 <br/>
</div>
