ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzajp_5.4.0.1/rzajpwhen.htm

124 lines
7.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 lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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="DC.Type" content="reference" />
<meta name="DC.Title" content="Handle exception conditions with the WHENEVER Statement" />
<meta name="abstract" content="The WHENEVER statement causes SQL to check the SQLSTATE and SQLCODE and continue processing your program, or branch to another area in your program if an error, exception, or warning exists as a result of running an SQL statement." />
<meta name="description" content="The WHENEVER statement causes SQL to check the SQLSTATE and SQLCODE and continue processing your program, or branch to another area in your program if an error, exception, or warning exists as a result of running an SQL statement." />
<meta name="DC.subject" content="handling, exception condition (WHENEVER statement), exception condition, WHENEVER statement, handling exception condition with, statements, WHENEVER, handling exception condition" />
<meta name="keywords" content="handling, exception condition (WHENEVER statement), exception condition, WHENEVER statement, handling exception condition with, statements, WHENEVER, handling exception condition" />
<meta name="DC.Relation" scheme="URI" content="rzajpsqlcom.htm" />
<meta name="DC.Relation" scheme="URI" content="rzajprexx.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rzajpwhen" />
<meta name="DC.Language" 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. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Handle exception conditions with the WHENEVER Statement</title>
</head>
<body id="rzajpwhen"><a name="rzajpwhen"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Handle exception conditions with the WHENEVER Statement</h1>
<div><p>The WHENEVER statement causes SQL to check the SQLSTATE and SQLCODE
and continue processing your program, or branch to another area in your program
if an error, exception, or warning exists as a result of running an SQL statement.</p>
<div class="section"><div class="p">An exception condition handling subroutine (part of your program)
can then examine the SQLCODE or SQLSTATE field to take an action specific
to the error or exception situation. <div class="note"><span class="notetitle">Note:</span> The WHENEVER statement is not allowed
in REXX procedures. </div>
</div>
</div>
<div class="section"><p>The WHENEVER statement allows you to specify what you want to
do whenever a general condition is true. You can specify more than one WHENEVER
statement for the same condition. When you do this, the first WHENEVER statement
applies to all subsequent SQL statements in the source program until another
WHENEVER statement is specified.</p>
</div>
<div class="section"><p>The WHENEVER statement looks like this: </p>
<pre>EXEC SQL
<strong>WHENEVER</strong> condition action
END-EXEC.</pre>
</div>
<div class="section"><p>There are three conditions you can specify: </p>
<dl><dt class="dlterm">SQLWARNING</dt>
<dd>Specify SQLWARNING to indicate what you want done when SQLWARN0 = W or
SQLCODE contains a positive value other than 100 (SUBSTR(SQLSTATE,1,2) ='01'). <div class="note"><span class="notetitle">Note:</span> SQLWARN0
could be set for several different reasons. For example, if the value of a
column was truncated when it was moved into a host variable, your program
might not regard this as an error.</div>
</dd>
<dt class="dlterm">SQLERROR</dt>
<dd>Specify SQLERROR to indicate what you want done when an error code is
returned as the result of an SQL statement (SQLCODE &lt; 0) (SUBSTR(SQLSTATE,1,2) &gt;
'02').</dd>
<dt class="dlterm">NOT FOUND</dt>
<dd>Specify NOT FOUND to indicate what you want done when an SQLCODE of +100
and a SQLSTATE of '02000' is returned because: <ul><li>After a single-row SELECT is issued or after the first FETCH is issued
for a cursor, the data the program specifies does not exist.</li>
<li>After a subsequent FETCH, no more rows satisfying the cursor select-statement
are left to retrieve.</li>
<li>After an UPDATE, a DELETE, or an INSERT, no row meets the search condition.</li>
</ul>
</dd>
</dl>
</div>
<div class="section"><p>You can also specify the action you want taken: </p>
<dl><dt class="dlterm">CONTINUE</dt>
<dd>This causes your program to continue to the next statement.</dd>
<dt class="dlterm">GO TO label</dt>
<dd>This causes your program to branch to an area in the program. The label
for that area may be preceded with a colon. The WHENEVER ... GO TO statement: <ul><li>Must be a section name or an unqualified paragraph name in COBOL</li>
<li>Is a label in PL/I and C</li>
<li>Is the label of a TAG in RPG</li>
</ul>
</dd>
</dl>
</div>
<div class="section"><div class="p">For example, if you are retrieving rows using a cursor, you expect
that SQL will eventually be unable to find another row when the FETCH statement
is issued. To prepare for this situation, specify a WHENEVER NOT FOUND GO
TO ... statement to cause SQL to branch to a place in the program where you
issue a CLOSE statement in order to close the cursor properly. <div class="note"><span class="notetitle">Note:</span> A WHENEVER
statement affects all subsequent <em>source</em> SQL statements until another
WHENEVER is encountered.</div>
</div>
</div>
<div class="section"><p>In other words, all SQL statements coded between two WHENEVER
statements (or following the first, if there is only one) are governed by
the first WHENEVER statement, regardless of the path the program takes.</p>
</div>
<div class="section"><p>Because of this, the WHENEVER statement <em>must precede</em> the
first SQL statement it is to affect. If the WHENEVER <em>follows</em> the SQL
statement, the branch is not taken on the basis of the value of the SQLCODE
and SQLSTATE set by that SQL statement. However, if your program checks the
SQLCODE or SQLSTATE directly, the check must be done after the SQL statement
is run.</p>
</div>
<div class="section"><p>The WHENEVER statement does not provide a CALL to a subroutine
option. For this reason, you might want to examine the SQLCODE or SQLSTATE
value after each SQL statement is run and call a subroutine, rather than use
a WHENEVER statement.</p>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzajpsqlcom.htm" title="This topic describes some concepts and rules that are common to using SQL statements in a host language.">Common concepts and rules for using embedded SQL</a></div>
</div>
<div class="relconcepts"><strong>Related concepts</strong><br />
<div><a href="rzajprexx.htm" title="REXX procedures do not have to be preprocessed. At run time, the REXX interpreter passes statements that it does not understand to the current active command environment for processing.">Code SQL statements in REXX applications</a></div>
</div>
</div>
</body>
</html>