ibm-information-center/dist/eclipse/plugins/i5OS.ic.db2_5.4.0.1/rbafzmstcasestmt.htm

140 lines
8.3 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="utf-8"?>
<!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" lang="en-US" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="dc.language" scheme="rfc1766" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<meta name="dc.date" scheme="iso8601" content="2005-09-19" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow"/>
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="keywords" content="CASE statement, SQL-control-statement,
SQL statements" />
<title>CASE statement</title>
<link rel="stylesheet" type="text/css" href="ibmidwb.css" />
<link rel="stylesheet" type="text/css" href="ic.css" />
</head>
<body>
<a id="Top_Of_Page" name="Top_Of_Page"></a><!-- Java sync-link -->
<script language = "Javascript" src = "../rzahg/synch.js" type="text/javascript"></script>
<a name="casestmt"></a>
<h2 id="casestmt"><a href="rbafzmst02.htm#ToC_1420">CASE statement</a></h2><a id="idx3337" name="idx3337"></a><a id="idx3338" name="idx3338"></a>
<p>The CASE statement selects an execution path based on multiple conditions.</p>
<a name="wq1860"></a>
<h3 id="wq1860"><a href="rbafzmst02.htm#ToC_1421">Syntax</a></h3>
<a href="rbafzmstcasestmt.htm#synccase"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn409.htm"
border="0" /></span><a href="#skipsyn-408"><img src="c.gif" alt="Skip visual syntax diagram"
border="0" /></a>>>-+--------+--CASE--+-<span class="italic">simple-when-clause</span>---+------------------->
'-<span class="italic">label:</span>-' '-<span class="italic">searched-when-clause</span>-'
>--+-------------+--END CASE----------------------------------->&lt;
'-<span class="italic">else-clause</span>-'
simple-when-clause:
|--<span class="italic">expression</span>--------------------------------------------------->
.---------------------------------------------------------.
| .-----------------------------. |
V V | |
>----WHEN--<span class="italic">expression</span>--THEN----<span class="italic">SQL-procedure-statement</span>-- ;-+-+--|
searched-when-clause:
.---------------------------------------------------------------.
| .-----------------------------. |
V V | |
|----WHEN--<span class="italic">search-condition</span>--THEN----<span class="italic">SQL-procedure-statement</span>-- ;-+-+--|
else-clause:
.-----------------------------.
V |
|--ELSE----<span class="italic">SQL-procedure-statement</span>-- ;-+------------------------|
</pre>
<a name="skipsyn-408" id="skipsyn-408"></a>
<a name="synccase"></a>
<h3 id="synccase"><a href="rbafzmst02.htm#ToC_1422">Description</a></h3>
<dl class="parml">
<dt class="bold"><span class="italic">label</span></dt>
<dd>Specifies the label for the CASE statement. The label name
cannot be the same as another label within the same scope. For more information,
see <a href="rbafzmstsqlprocstmt.htm#psscope">Labels</a>.
</dd>
<dt class="bold"><span class="italic">simple-when-clause</span></dt>
<dd>The value of the <var class="pv">expression</var> prior to the first WHEN keyword
is tested for equality with the value of each <var class="pv">expression</var> that follows
the WHEN keyword. If the comparison is true, the THEN statement is executed.
If the result is unknown or false, processing continues to the next comparison.
If the result does not match any of the comparisons, and an ELSE clause is
present, the statements in the ELSE clause are processed.
</dd>
<dt class="bold"><span class="italic">searched-when-clause</span></dt>
<dd>The <var class="pv">search-condition</var> following the WHEN keyword is evaluated.
If it evaluates to true, the statements in the associated THEN clause are
processed. If it evaluates to false, or unknown, the next <var class="pv">search-condition</var> is evaluated. If no <var class="pv">search-condition</var> evaluates to true and
an ELSE clause is present, the statements in the ELSE clause are processed.
</dd>
<dt class="bold"><span class="italic">else-clause</span></dt>
<dd>If none of the conditions specified in the <span class="italic">simple-when-clause</span> or <span class="italic">searched-when-clause</span> are true, then the statements
in the <span class="italic">else-clause</span> are executed.
<p>If none of the
conditions specified in the WHEN are true, and an ELSE clause is not specified,
an error is issued at runtime, and the execution of the CASE statement is
terminated (SQLSTATE 20000).</p>
</dd>
<dt class="bold"><span class="italic">SQL-procedure-statement</span></dt>
<dd>Specifies a statement that should be executed. See <a href="rbafzmstsqlprocstmt.htm#sqlprocstmt">SQL-procedure-statement</a>.
</dd>
</dl>
<a name="wq1861"></a>
<h3 id="wq1861"><a href="rbafzmst02.htm#ToC_1423">Notes</a></h3>
<p><span class="bold">Nesting of CASE statements:</span> CASE statements that
use a <span class="italic">simple-when-clause</span> can be nested up to three
levels. CASE statements that use a <span class="italic">searched-when-clause</span> have no limit to the number of nesting levels.</p>
<a name="wq1862"></a>
<h3 id="wq1862"><a href="rbafzmst02.htm#ToC_1424">Examples</a></h3>
<p><span class="italic">Example 1:</span> Depending on the value of SQL variable
v_workdept, update column DEPTNAME in table DEPARTMENT with the appropriate
name.</p>
<p>The following example shows how to do this using the syntax for a <span class="italic">simple-when-clause</span>.</p>
<pre class="xmp"> <span class="bold">CASE</span> v_workdept
<span class="bold">WHEN</span> 'A00'
<span class="bold">THEN UPDATE</span> department <span class="bold">SET</span>
deptname = 'DATA ACCESS 1';
<span class="bold">WHEN</span> 'B01'
<span class="bold">THEN UPDATE</span> department <span class="bold">SET</span>
deptname = 'DATA ACCESS 2';
<span class="bold">ELSE UPDATE</span> department <span class="bold">SET</span>
deptname = 'DATA ACCESS 3';
<span class="bold">END CASE</span> </pre>
<p><span class="italic">Example 2:</span> The following example shows how to do
this using the syntax for a <span class="italic">searched-when-clause</span>:</p>
<pre class="xmp"> <span class="bold">CASE</span>
<span class="bold">WHEN</span> v_workdept = 'A00'
<span class="bold">THEN UPDATE</span> department <span class="bold">SET</span>
deptname = 'DATA ACCESS 1';
<span class="bold">WHEN</span> v_workdept = 'B01'
<span class="bold">THEN UPDATE</span> department <span class="bold">SET</span>
deptname = 'DATA ACCESS 2';
<span class="bold">ELSE UPDATE</span> department <span class="bold">SET</span>
deptname = 'DATA ACCESS 3';
<span class="bold">END CASE</span></pre>
<hr /><br />
[ <a href="#Top_Of_Page">Top of Page</a> | <a href="rbafzmstcallstmt.htm">Previous Page</a> | <a href="rbafzmstcompoundstmt.htm">Next Page</a> | <a href="rbafzmst02.htm#wq1">Contents</a> |
<a href="rbafzmstindex.htm#index">Index</a> ]
<a id="Bot_Of_Page" name="Bot_Of_Page"></a>
</body>
</html>