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

109 lines
6.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="GOTO statement, SQL-control-statement,
SQL statements" />
<title>GOTO 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="gotostmt"></a>
<h2 id="gotostmt"><a href="rbafzmst02.htm#ToC_1440">GOTO statement</a></h2><a id="idx3352" name="idx3352"></a><a id="idx3353" name="idx3353"></a>
<p>The GOTO statement branches to a user-defined label within an SQL function,
SQL procedure, or SQL trigger.</p>
<a name="wq1881"></a>
<h3 id="wq1881"><a href="rbafzmst02.htm#ToC_1441">Syntax</a></h3>
<a href="rbafzmstgotostmt.htm#syncgoto"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn416.htm"
border="0" /></span><a href="#skipsyn-415"><img src="c.gif" alt="Skip visual syntax diagram"
border="0" /></a>>>-+---------+--GOTO--<span class="italic">label2</span>----------------------------------->&lt;
'-<span class="italic">label1:</span>-'
</pre>
<a name="skipsyn-415" id="skipsyn-415"></a>
<a name="syncgoto"></a>
<h3 id="syncgoto"><a href="rbafzmst02.htm#ToC_1442">Description</a></h3>
<dl class="parml">
<dt class="bold"><span class="italic">label1</span></dt>
<dd>Specifies the label for the GOTO 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 labelled statement where processing is to continue. The
labelled statement and the GOTO statement must both be in the same scope:
<ul>
<li>If the GOTO statement is defined in a FOR statement, <span class="italic">label</span> must be defined inside the same FOR statement, excluding a nested
FOR statement or nested compound statement.</li>
<li>If the GOTO statement is defined outside a FOR statement, <span class="italic">label</span> must not be defined within a FOR statement or nested compound statement.</li>
<li>If the GOTO statement is defined in a handler, <span class="italic">label</span> must be defined inside the same handler.</li>
<li>If the GOTO statement is defined outside a handler, <span class="italic">label</span> must not be defined within a handler.</li></ul>
<p>If <span class="italic">label2</span> is not defined within a scope that
the GOTO statement can reach, an error is returned.</p>
</dd>
</dl>
<a name="wq1882"></a>
<h3 id="wq1882"><a href="rbafzmst02.htm#ToC_1443">Notes</a></h3>
<p><span class="bold">Using a GOTO statement</span>: Use the GOTO statement sparingly.
This statement interferes with the normal sequence of processing, thus making
a routine more difficult to read and maintain. Often, another statement, such
as IF or LEAVE, can eliminate the need for a GOTO statement.</p>
<a name="wq1883"></a>
<h3 id="wq1883"><a href="rbafzmst02.htm#ToC_1444">Example</a></h3>
<p>In the following statement, the parameters <span class="italic">rating</span> and <span class="italic">v_empno</span> are passed in to the procedure. The time in service
is returned as a date duration in output parameter <span class="italic">return_parm</span>. If the time in service with the company is less then 6 months, the
GOTO statement transfers control to the end of the procedure and <span class="italic">new_salary</span> is left unchanged.</p>
<pre class="xmp"><span class="bold">CREATE PROCEDURE</span> adjust_salary
<span class="bold">(IN</span> v_empno <span class="bold">CHAR</span>(6),
<span class="bold">IN</span> rating <span class="bold">INTEGER</span>,
<span class="bold">OUT</span> return_parm <span class="bold">DECIMAL</span>(8,2)<span class="bold">)</span>
<span class="bold">LANGUAGE SQL
MODIFIES SQL DATA</span>
<span class="bold">BEGIN</span>
<span class="bold">DECLARE</span> new_salary <span class="bold">DECIMAL</span>(9,2);
<span class="bold">DECLARE</span> service <span class="bold">DECIMAL</span>(8,2);
<span class="bold">SELECT</span> salary, <span class="bold">CURRENT_DATE</span> - hiredate
<span class="bold">INTO</span> new_salary, service
<span class="bold">FROM</span> employee
<span class="bold">WHERE</span> empno = v_empno;
<span class="bold">IF</span> service &lt; 600
<span class="bold">THEN GOTO</span> exit1;
<span class="bold">END IF</span>;
<span class="bold">IF</span> rating = 1
<span class="bold">THEN SET</span> new_salary = new_salary + (new_salary * .10);
<span class="bold">ELSEIF</span> rating = 2
<span class="bold">THEN SET</span> new_salary = new_salary + (new_salary * .05);
<span class="bold">END IF</span>;
<span class="bold">UPDATE</span> employee
<span class="bold">SET</span> salary = new_salary
<span class="bold">WHERE</span> empno = v_empno;
EXIT1: <span class="bold">SET</span> return_parm = service;
<span class="bold">END</span>
</pre>
<hr /><br />
[ <a href="#Top_Of_Page">Top of Page</a> | <a href="rbafzmstgetdiag2.htm">Previous Page</a> | <a href="rbafzmstifstmt.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>