104 lines
6.0 KiB
HTML
104 lines
6.0 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="WHILE statement, SQL-control-statement,
|
|
SQL statements" />
|
|
<title>WHILE 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="whilestmt"></a>
|
|
<h2 id="whilestmt"><a href="rbafzmst02.htm#ToC_1481">WHILE statement</a></h2><a id="idx3374" name="idx3374"></a><a id="idx3375" name="idx3375"></a>
|
|
<p>The WHILE statement repeats the execution of a statement while a specified
|
|
condition is true.</p>
|
|
<a name="wq1904"></a>
|
|
<h3 id="wq1904"><a href="rbafzmst02.htm#ToC_1482">Syntax</a></h3>
|
|
<a href="rbafzmstwhilestmt.htm#syncwhile"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn425.htm"
|
|
border="0" /></span><a href="#skipsyn-424"><img src="c.gif" alt="Skip visual syntax diagram"
|
|
border="0" /></a>>>-+--------+--WHILE--<span class="italic">search-condition</span>--DO---------------------->
|
|
'-<span class="italic">label:</span>-'
|
|
|
|
.-----------------------------.
|
|
V |
|
|
>----<span class="italic">SQL-procedure-statement</span>-- ;-+--END WHILE--+-------+-------><
|
|
'-<span class="italic">label</span>-'
|
|
|
|
</pre>
|
|
<a name="skipsyn-424" id="skipsyn-424"></a>
|
|
<a name="syncwhile"></a>
|
|
<h3 id="syncwhile"><a href="rbafzmst02.htm#ToC_1483">Description</a></h3>
|
|
<dl class="parml">
|
|
<dt class="bold"><span class="italic">label</span></dt>
|
|
<dd>Specifies the label for the WHILE statement. If the ending
|
|
label is specified, it must be the same as the beginning label. 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 a condition that is evaluated before each execution of the
|
|
WHILE loop. If the condition is true, the <span class="italic">SQL-procedure-statements</span> in the WHILE loop are executed.
|
|
</dd>
|
|
<dt class="bold"><span class="italic">SQL-procedure-statement</span></dt>
|
|
<dd>Specifies an SQL statement or statements to execute within the WHILE
|
|
loop.
|
|
</dd>
|
|
</dl>
|
|
<a name="wq1905"></a>
|
|
<h3 id="wq1905"><a href="rbafzmst02.htm#ToC_1484">Example</a></h3>
|
|
<p>This example uses a WHILE statement to iterate through FETCH and SET statements.
|
|
While the value of SQL variable <span class="italic">v_counter</span> is less
|
|
than half of number of employees in the department identified by the IN parameter <span class="italic">deptNumber</span>, the WHILE statement continues to perform the
|
|
FETCH and SET statements. When the condition is no longer true, the flow of
|
|
control leaves the WHILE statement and closes the cursor.</p>
|
|
<pre class="xmp"><span class="bold">CREATE PROCEDURE</span> dept_median
|
|
<span class="bold">(IN</span> deptNumber <span class="bold">SMALLINT,
|
|
OUT</span> medianSalary <span class="bold">DECIMAL(</span>7,2<span class="bold">))</span>
|
|
<span class="bold">LANGUAGE SQL</span>
|
|
<span class="bold">BEGIN</span>
|
|
<span class="bold">DECLARE</span> v_numRecords <span class="bold">INTEGER DEFAULT</span> 1;
|
|
<span class="bold">DECLARE</span> v_counter <span class="bold">INTEGER DEFAULT</span> 0;
|
|
<span class="bold">DECLARE</span> c1 <span class="bold">CURSOR FOR</span>
|
|
<span class="bold">SELECT</span> salary
|
|
<span class="bold">FROM</span> staff
|
|
<span class="bold">WHERE</span> dept = deptNumber
|
|
<span class="bold">ORDER BY</span> salary;
|
|
<span class="bold">DECLARE EXIT HANDLER FOR NOT FOUND</span>
|
|
<span class="bold">SET</span> medianSalary = 6666;
|
|
<span class="bold">SET</span> medianSalary = 0;
|
|
<span class="bold">SELECT COUNT(*) INTO</span> v_numRecords
|
|
<span class="bold">FROM</span> staff
|
|
<span class="bold">WHERE</span> dept = deptNumber;
|
|
<span class="bold">OPEN</span> c1;
|
|
<span class="bold">WHILE</span> v_counter < (v_numRecords/2 + 1) <span class="bold">DO</span>
|
|
<span class="bold">FETCH</span> c1 <span class="bold">INTO</span> medianSalary;
|
|
<span class="bold">SET</span> v_counter = v_counter +1;
|
|
<span class="bold">END WHILE</span>;
|
|
<span class="bold">CLOSE</span> c1;
|
|
<span class="bold">END</span></pre>
|
|
<hr /><br />
|
|
[ <a href="#Top_Of_Page">Top of Page</a> | <a href="rbafzmstsignalstmt.htm">Previous Page</a> | <a href="rbafzmstlimtabs.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>
|