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

99 lines
5.8 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="REPEAT statement, SQL-control-statement,
SQL statements" />
<title>REPEAT 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="repeatstmt"></a>
<h2 id="repeatstmt"><a href="rbafzmst02.htm#ToC_1462">REPEAT statement</a></h2><a id="idx3362" name="idx3362"></a><a id="idx3363" name="idx3363"></a>
<p>The REPEAT statement executes a statement or group of statements until
a search condition is true.</p>
<a name="wq1893"></a>
<h3 id="wq1893"><a href="rbafzmst02.htm#ToC_1463">Syntax</a></h3>
<a href="rbafzmstrepeatstmt.htm#syncrepeat"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn421.htm"
border="0" /></span><a href="#skipsyn-420"><img src="c.gif" alt="Skip visual syntax diagram"
border="0" /></a> .-----------------------------.
V |
>>-+--------+--REPEAT----<span class="italic">SQL-procedure-statement</span>-- ;-+---------->
'-<span class="italic">label:</span>-'
>--UNTIL--<span class="italic">search-condition</span>--END REPEAT--+-------+-------------->&lt;
'-<span class="italic">label</span>-'
</pre>
<a name="skipsyn-420" id="skipsyn-420"></a>
<a name="syncrepeat"></a>
<h3 id="syncrepeat"><a href="rbafzmst02.htm#ToC_1464">Description</a></h3>
<dl class="parml">
<dt class="bold"><span class="italic">label</span></dt>
<dd>Specifies the label for the REPEAT 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 REPEAT loop.
</dd>
<dt class="bold"><span class="italic">search-condition</span></dt>
<dd>The <span class="italic">search-condition</span> is evaluated after each
execution of the REPEAT loop. If the condition is true, the REPEAT loop will
exit. If the condition is unknown or false, the looping continues.
</dd>
</dl>
<a name="wq1894"></a>
<h3 id="wq1894"><a href="rbafzmst02.htm#ToC_1465">Example</a></h3>
<p>A REPEAT statement fetches rows from a table until the <span class="italic">not_found</span> condition handler is invoked.</p>
<pre class="xmp"> <span class="bold">CREATE PROCEDURE</span> REPEAT_STMT <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> at_end <span class="bold">SMALLINT 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> firstnme, midinit, lastname
<span class="bold">FROM</span> employee;
<span class="bold">DECLARE CONTINUE HANDLER FOR</span> not_found
<span class="bold">SET</span> at_end = 1;
<span class="bold">OPEN</span> c1;
fetch_loop:
<span class="bold">REPEAT</span>
<span class="bold">FETCH</span> c1 <span class="bold">INTO</span> v_firstnme, v_midinit, v_lastname;
<span class="bold">SET</span> v_counter = v_counter + 1;
<span class="bold">UNTIL</span> at_end > 0
<span class="bold">END REPEAT</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="rbafzmstloopstmt.htm">Previous Page</a> | <a href="rbafzmstresignalstmt.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>