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

97 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="LEAVE statement, SQL-control-statement,
SQL statements" />
<title>LEAVE 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="leavestmt"></a>
<h2 id="leavestmt"><a href="rbafzmst02.htm#ToC_1453">LEAVE statement</a></h2><a id="idx3358" name="idx3358"></a><a id="idx3359" name="idx3359"></a>
<p>The LEAVE statement continues execution by leaving a block or loop.</p>
<a name="wq1888"></a>
<h3 id="wq1888"><a href="rbafzmst02.htm#ToC_1454">Syntax</a></h3>
<a href="rbafzmstleavestmt.htm#syncleave"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn419.htm"
border="0" /></span><a href="#skipsyn-418"><img src="c.gif" alt="Skip visual syntax diagram"
border="0" /></a>>>-+---------+--LEAVE--<span class="italic">label2</span>---------------------------------->&lt;
'-<span class="italic">label1:</span>-'
</pre>
<a name="skipsyn-418" id="skipsyn-418"></a>
<a name="syncleave"></a>
<h3 id="syncleave"><a href="rbafzmst02.htm#ToC_1455">Description</a></h3>
<dl class="parml">
<dt class="bold"><span class="italic">label1</span></dt>
<dd>Specifies the label for the LEAVE 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 compound, FOR, LOOP, REPEAT, or WHILE statement
to exit.
</dd>
</dl>
<a name="wq1889"></a>
<h3 id="wq1889"><a href="rbafzmst02.htm#ToC_1456">Notes</a></h3>
<p><span class="bold">Effect on open cursors:</span> When a LEAVE statement transfers
control out of a compound statement, all open cursors in the compound statement,
except cursors that are used to return result sets, are closed.</p>
<a name="wq1890"></a>
<h3 id="wq1890"><a href="rbafzmst02.htm#ToC_1457">Examples</a></h3>
<p>The example contains a loop that fetches data for cursor <span class="italic">c1</span>. If the value of SQL variable <span class="italic">at_end</span> is not
zero, the LEAVE statement transfers control out of the loop.</p>
<pre class="xmp"> <span class="bold">CREATE PROCEDURE</span> LEAVE_LOOP <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</span>;
<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">SET</span> v_counter = 0;
<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> at_end &lt;> 0 <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="rbafzmstiteratestmt.htm">Previous Page</a> | <a href="rbafzmstloopstmt.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>