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

95 lines
5.7 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="LOOP statement, SQL-control-statement,
SQL statements" />
<title>LOOP 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="loopstmt"></a>
<h2 id="loopstmt"><a href="rbafzmst02.htm#ToC_1458">LOOP statement</a></h2><a id="idx3360" name="idx3360"></a><a id="idx3361" name="idx3361"></a>
<p>The LOOP statement repeats the execution of a statement or a group of statements.</p>
<a name="wq1891"></a>
<h3 id="wq1891"><a href="rbafzmst02.htm#ToC_1459">Syntax</a></h3>
<a href="rbafzmstloopstmt.htm#syncloop"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn420.htm"
border="0" /></span><a href="#skipsyn-419"><img src="c.gif" alt="Skip visual syntax diagram"
border="0" /></a> .-----------------------------.
V |
>>-+--------+--LOOP----<span class="italic">SQL-procedure-statement</span>-- ;-+------------>
'-<span class="italic">label:</span>-'
>--END LOOP--+-------+----------------------------------------->&lt;
'-<span class="italic">label</span>-'
</pre>
<a name="skipsyn-419" id="skipsyn-419"></a>
<a name="syncloop"></a>
<h3 id="syncloop"><a href="rbafzmst02.htm#ToC_1460">Description</a></h3>
<dl class="parml">
<dt class="bold"><span class="italic">label</span></dt>
<dd>Specifies the label for the LOOP 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">SQL-procedure statement</span></dt>
<dd>Specifies an SQL statement to be executed in the loop
</dd>
</dl>
<a name="wq1892"></a>
<h3 id="wq1892"><a href="rbafzmst02.htm#ToC_1461">Examples</a></h3>
<p>This procedure uses a LOOP statement to fetch values from the employee
table. Each time the loop iterates, the OUT parameter <span class="italic">counter</span> is incremented and the value of <span class="italic">v_midinit</span> is checked to ensure that the value is not a single space (' '). If <span class="italic">v_midinit</span> is a single space, the LEAVE statement passes
the flow of control outside of the loop.</p>
<pre class="xmp"> <span class="bold">CREATE PROCEDURE</span> LOOP_UNTIL_SPACE <span class="bold">(OUT</span> COUNTER <span class="bold">INTEGER)</span>
<span class="bold">LANGUAGE SQL</span>
<span class="bold">BEGIN</span>
<span class="bold">DECLARE</span> v_counter <span class="bold">INTEGER DEFAULT </span>0;
<span class="bold">DECLARE</span> v_firstnme <span class="bold">VARCHAR(</span>12<span class="bold">)</span>;
<span class="bold">DECLARE</span> v_midinit <span class="bold">CHAR(</span>1<span class="bold">)</span>;
<span class="bold">DECLARE</span> v_lastname <span class="bold">VARCHAR(</span>15<span class="bold">)</span>;
<span class="bold">DECLARE</span> c1 <span class="bold">CURSOR FOR</span>
<span class="bold">SELECT</span> firstnme, midinit, lastname
<span class="bold">FROM</span> employee;
<span class="bold">DECLARE CONTINUE HANDLER FOR NOT FOUND</span>
<span class="bold">SET</span> counter = -1;
<span class="bold">OPEN</span> c1;
fetch_loop:
<span class="bold">LOOP</span>
<span class="bold">FETCH</span> c1 <span class="bold">INTO</span> v_firstnme, v_midinit, v_lastname;
<span class="bold">IF</span> v_midinit = ' ' <span class="bold">THEN</span>
<span class="bold">LEAVE</span> fetch_loop;
<span class="bold">END IF</span>;
<span class="bold">SET</span> v_counter = v_counter + 1;
<span class="bold">END LOOP</span> fetch_loop;
<span class="bold">SET</span> counter = v_counter;
<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="rbafzmstleavestmt.htm">Previous Page</a> | <a href="rbafzmstrepeatstmt.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>