133 lines
7.8 KiB
HTML
133 lines
7.8 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="FOR statement, SQL-control-statement,
|
||
|
SQL statements, in FOR statement, WITH HOLD clause" />
|
||
|
<title>FOR 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="forstmt"></a>
|
||
|
<h2 id="forstmt"><a href="rbafzmst02.htm#ToC_1430">FOR statement</a></h2><a id="idx3341" name="idx3341"></a><a id="idx3342" name="idx3342"></a>
|
||
|
<p>The FOR statement executes a statement for each row of a table.</p>
|
||
|
<a name="wq1870"></a>
|
||
|
<h3 id="wq1870"><a href="rbafzmst02.htm#ToC_1431">Syntax</a></h3>
|
||
|
<a href="rbafzmstforstmt.htm#syncfor"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
||
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn412.htm"
|
||
|
border="0" /></span><a href="#skipsyn-411"><img src="c.gif" alt="Skip visual syntax diagram"
|
||
|
border="0" /></a>>>-+--------+--FOR--+-------------------+--AS------------------->
|
||
|
'-<span class="italic">label:</span>-' '-<span class="italic">SQL-variable-name</span>-'
|
||
|
|
||
|
>----<span class="italic">cursor-name</span>--CURSOR--+-----------+--FOR-------------------->
|
||
|
'-WITH HOLD-'
|
||
|
|
||
|
.-----------------------------.
|
||
|
V |
|
||
|
>--<span class="italic">select-statement</span>--DO----<span class="italic">SQL-procedure-statement</span>-- ;-+-------->
|
||
|
|
||
|
>--END FOR--+-------+------------------------------------------><
|
||
|
'-<span class="italic">label</span>-'
|
||
|
|
||
|
</pre>
|
||
|
<a name="skipsyn-411" id="skipsyn-411"></a>
|
||
|
<a name="syncfor"></a>
|
||
|
<h3 id="syncfor"><a href="rbafzmst02.htm#ToC_1432">Description</a></h3>
|
||
|
<dl class="parml">
|
||
|
<dt class="bold"><span class="italic">label</span></dt>
|
||
|
<dd>Specifies the label for the FOR 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-variable-name</span></dt>
|
||
|
<dd>The <span class="italic">SQL-variable-name</span> can be used
|
||
|
to qualify variables in the statement. The <span class="italic">SQL-variable-name</span> must not be the same as any label within the same scope. For more information,
|
||
|
see <a href="rbafzmstsqlprocstmt.htm#psscope">Labels</a>.
|
||
|
<p>Either the <span class="italic">SQL-variable-name</span> or <span class="italic">label</span> can be used to qualify other SQL variable
|
||
|
names in the statement.</p>
|
||
|
<p>If <span class="italic">SQL-variable-name</span> is
|
||
|
specified, then it should be used to qualify any other SQL variable names
|
||
|
in the statement when debugging the SQL function, SQL procedure, or SQL trigger.</p>
|
||
|
</dd>
|
||
|
<dt class="bold"><span class="italic">cursor-name</span></dt>
|
||
|
<dd>Names a cursor. If not specified, a unique cursor name is generated.
|
||
|
</dd>
|
||
|
<dt class="bold">WITH HOLD </dt><a id="idx3343" name="idx3343"></a>
|
||
|
<dd>Prevents the cursor from being closed as a consequence of a commit operation.
|
||
|
A cursor declared using the WITH HOLD clause is implicitly closed at commit
|
||
|
time only if the connection associated with the cursor is ended during the
|
||
|
commit operation. For more information, see <a href="rbafzmsth2clcu.htm#h2dclcu">DECLARE CURSOR</a>.
|
||
|
</dd>
|
||
|
<dt class="bold"><span class="italic">select-statement</span></dt>
|
||
|
<dd>Specifies the select statement of the cursor.
|
||
|
<p>Each expression in the
|
||
|
select list must have a name. If an expression is not a simple column name,
|
||
|
the AS clause must be used to name the expression. If the AS clause is specified,
|
||
|
that name is used for the variable and must be unique.</p>
|
||
|
</dd>
|
||
|
<dt class="bold"><span class="italic">SQL-procedure-statement</span></dt>
|
||
|
<dd>Specifies the SQL statements to be executed for each row of the table.
|
||
|
The SQL statements should not include an OPEN, FETCH, or CLOSE specifying
|
||
|
the cursor name of the FOR statement.
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<a name="wq1871"></a>
|
||
|
<h3 id="wq1871"><a href="rbafzmst02.htm#ToC_1433">Notes</a></h3>
|
||
|
<p><span class="bold">FOR statement rules</span>: The FOR statement executes one
|
||
|
or multiple statements for each row in a table. The cursor is defined by specifying
|
||
|
a select list that describes the columns and rows selected. The statements
|
||
|
within the FOR statement are executed for each row selected.</p>
|
||
|
<p>The select list must consist of unique column names and the table specified
|
||
|
in the select list must exist when the function, procedure, or trigger is
|
||
|
created.</p>
|
||
|
<p>The cursor specified in a FOR statement cannot be referenced outside the
|
||
|
FOR statement and cannot be specified on an OPEN, FETCH, or CLOSE statement.</p>
|
||
|
<p><span class="bold">Handler warning</span>: Handlers may be used to handle errors
|
||
|
that might occur on the open of the cursor or fetch of a row using the cursor
|
||
|
in the FOR statement. Handlers defined to handle these open or fetch conditions
|
||
|
should not be CONTINUE handlers as they may cause the FOR statement to loop
|
||
|
indefinitely.</p>
|
||
|
<a name="wq1872"></a>
|
||
|
<h3 id="wq1872"><a href="rbafzmst02.htm#ToC_1434">Example</a></h3>
|
||
|
<p>In this example, the FOR statement is used to specify a cursor that selects
|
||
|
3 columns from the <tt class="xph">employee</tt> table. For every row selected, SQL
|
||
|
variable <span class="italic">fullname</span> is set to the last name followed
|
||
|
by a comma, the first name, a blank, and the middle initial. Each value for <span class="italic">fullname</span> is inserted into table <tt class="xph">TNAMES</tt>.</p>
|
||
|
<pre class="xmp"> <span class="bold">BEGIN</span>
|
||
|
<span class="bold">DECLARE</span> fullname <span class="bold">CHAR</span>(40);
|
||
|
<span class="bold">FOR</span> vl <span class="bold">AS</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">DO</span>
|
||
|
<span class="bold">SET</span> fullname =
|
||
|
lastname || ', ' || firstnme ||' ' || midinit;
|
||
|
<span class="bold">INSERT INTO</span> TNAMES <span class="bold">VALUE</span> ( fullname );
|
||
|
<span class="bold">END FOR</span>;
|
||
|
<span class="bold">END</span>;
|
||
|
</pre>
|
||
|
<hr /><br />
|
||
|
[ <a href="#Top_Of_Page">Top of Page</a> | <a href="rbafzmstcompoundstmt.htm">Previous Page</a> | <a href="rbafzmstgetdiag2.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>
|