<?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="ITERATE statement, SQL-control-statement, SQL statements" /> <title>ITERATE 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="iteratestmt"></a> <h2 id="iteratestmt"><a href="rbafzmst02.htm#ToC_1449">ITERATE statement</a></h2><a id="idx3356" name="idx3356"></a><a id="idx3357" name="idx3357"></a> <p>The ITERATE statement causes the flow of control to return to the beginning of a labelled loop.</p> <a name="wq1886"></a> <h3 id="wq1886"><a href="rbafzmst02.htm#ToC_1450">Syntax</a></h3> <a href="rbafzmstiteratestmt.htm#synciterate"><img src="c.gif" alt="Click to skip syntax diagram" /></a> <pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn418.htm" border="0" /></span><a href="#skipsyn-417"><img src="c.gif" alt="Skip visual syntax diagram" border="0" /></a>>>-+---------+--ITERATE--<span class="italic">label2</span>-------------------------------->< '-<span class="italic">label1:</span>-' </pre> <a name="skipsyn-417" id="skipsyn-417"></a> <a name="synciterate"></a> <h3 id="synciterate"><a href="rbafzmst02.htm#ToC_1451">Description</a></h3> <dl class="parml"> <dt class="bold"><span class="italic">label1</span></dt> <dd>Specifies the label for the ITERATE 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">label2</span></dt> <dd>Specifies the label of the FOR, LOOP, REPEAT, or WHILE statement to which the database manager passes the flow of control. </dd> </dl> <a name="wq1887"></a> <h3 id="wq1887"><a href="rbafzmst02.htm#ToC_1452">Example</a></h3> <p>This example uses a cursor to return information for a new department. If the <span class="italic">not_found</span> condition handler was invoked, the flow of control passes out of the loop. If the value of <span class="italic">v_dept</span> is 'D11', an ITERATE statement passes the flow of control back to the top of the LOOP statement. Otherwise, a new row is inserted into the DEPARTMENT table.</p> <pre class="xmp"> <span class="bold">CREATE PROCEDURE</span> ITERATOR () <span class="bold">LANGUAGE SQL</span> <span class="bold">MODIFIES SQL DATA</span> <span class="bold">BEGIN</span> <span class="bold">DECLARE</span> v_dept <span class="bold">CHAR</span>(3); <span class="bold">DECLARE</span> v_deptname <span class="bold">VARCHAR</span>(29); <span class="bold">DECLARE</span> v_admdept <span class="bold">CHAR</span>(3); <span class="bold">DECLARE</span> at_end <span class="bold">INTEGER DEFAULT</span> 0; <span class="bold">DECLARE</span> not_found <span class="bold">CONDITION FOR SQLSTATE</span> '02000'; <span class="bold">DECLARE</span> c1 <span class="bold">CURSOR FOR</span> <span class="bold">SELECT</span> deptno,deptname,admrdept <span class="bold">FROM</span> department <span class="bold">ORDER BY</span> deptno; <span class="bold">DECLARE CONTINUE HANDLER FOR</span> not_found <span class="bold">SET</span> at_end = 1; <span class="bold">OPEN</span> c1; ins_loop: <span class="bold">LOOP</span> <span class="bold">FETCH</span> c1 <span class="bold">INTO</span> v_dept, v_deptname, v_admdept; <span class="bold">IF</span> at_end = 1 <span class="bold">THEN</span> <span class="bold">LEAVE</span> ins_loop; <span class="bold">ELSEIF</span> v_dept ='D11' <span class="bold">THEN</span> <span class="bold">ITERATE</span> ins_loop; <span class="bold">END IF</span>; <span class="bold">INSERT INTO</span> department (deptno,deptname,admrdept) <span class="bold">VALUES</span>('NEW', v_deptname, v_admdept); <span class="bold">END LOOP</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="rbafzmstifstmt.htm">Previous Page</a> | <a href="rbafzmstleavestmt.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>