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

109 lines
6.2 KiB
HTML

<?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="IF statement, SQL-control-statement,
SQL statements" />
<title>IF 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="ifstmt"></a>
<h2 id="ifstmt"><a href="rbafzmst02.htm#ToC_1445">IF statement</a></h2><a id="idx3354" name="idx3354"></a><a id="idx3355" name="idx3355"></a>
<p>The IF statement executes different sets of SQL statements based on the
result of search conditions.</p>
<a name="wq1884"></a>
<h3 id="wq1884"><a href="rbafzmst02.htm#ToC_1446">Syntax</a></h3>
<a href="rbafzmstifstmt.htm#syncif"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn417.htm"
border="0" /></span><a href="#skipsyn-416"><img src="c.gif" alt="Skip visual syntax diagram"
border="0" /></a>>>-+--------+--IF--<span class="italic">search-condition</span>--THEN----------------------->
'-<span class="italic">label:</span>-'
.-----------------------------.
V |
>----<span class="italic">SQL-procedure-statement</span>-- ;-+------------------------------>
.---------------------------------------------------------------------.
V |
>----+-----------------------------------------------------------------+-+-->
| .-----------------------------. |
| V | |
'-ELSEIF--<span class="italic">search-condition</span>--THEN----<span class="italic">SQL-procedure-statement</span>-- ;-+-'
>--+---------------------------------------+--END IF----------->&lt;
| .-----------------------------. |
| V | |
'-ELSE----<span class="italic">SQL-procedure-statement</span>-- ;-+-'
</pre>
<a name="skipsyn-416" id="skipsyn-416"></a>
<a name="syncif"></a>
<h3 id="syncif"><a href="rbafzmst02.htm#ToC_1447">Description</a></h3>
<dl class="parml">
<dt class="bold"><span class="italic">label</span></dt>
<dd>Specifies the label for the IF 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">search-condition</span></dt>
<dd>Specifies the <var class="pv">search-condition</var> for which an SQL statement should
be executed. If the condition is unknown or false, processing continues to
the next search condition, until either a condition is true or processing
reaches the ELSE clause.
</dd>
<dt class="bold"><span class="italic">SQL-procedure-statement</span></dt>
<dd>Specifies an SQL statement that should be executed if the preceding <span class="italic">search-condition</span> is true.
</dd>
</dl>
<a name="wq1885"></a>
<h3 id="wq1885"><a href="rbafzmst02.htm#ToC_1448">Example</a></h3>
<p>The following SQL procedure accepts two IN parameters: an employee number
and an employee rating. Depending on the value of <span class="italic">rating</span>, the employee table is updated with new values in the salary and bonus
columns.</p>
<pre class="xmp"><span class="bold">CREATE PROCEDURE</span> UPDATE_SALARY_IF
<span class="bold">(IN</span> employee_number <span class="bold">CHAR</span>(6), <span class="bold">INOUT</span> rating <span class="bold">SMALLINT)</span>
<span class="bold">LANGUAGE SQL</span>
<span class="bold">MODIFIES SQL DATA</span>
<span class="bold">BEGIN</span>
<span class="bold">DECLARE</span> not_found <span class="bold">CONDITION FOR SQLSTATE</span> '02000';
<span class="bold">DECLARE EXIT HANDLER FOR</span> not_found
<span class="bold">SET</span> rating = -1;
<span class="bold">IF</span> rating = 1
<span class="bold">THEN UPDATE</span> employee
<span class="bold">SET</span> salary = salary * 1.10, bonus = 1000
<span class="bold">WHERE</span> empno = employee_number;
<span class="bold">ELSEIF</span> rating = 2
<span class="bold">THEN UPDATE</span> employee
<span class="bold">SET</span> salary = salary * 1.05, bonus = 500
<span class="bold">WHERE</span> empno = employee_number;
<span class="bold">ELSE UPDATE</span> employee
<span class="bold">SET</span> salary = salary * 1.03, bonus = 0
<span class="bold">WHERE</span> empno = employee_number;
<span class="bold">END IF</span>;
<span class="bold">END</span> </pre>
<hr /><br />
[ <a href="#Top_Of_Page">Top of Page</a> | <a href="rbafzmstgotostmt.htm">Previous Page</a> | <a href="rbafzmstiteratestmt.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>