ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzajq_5.4.0.1/retrs.htm

124 lines
7.3 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="General rules for retaining cursor positions for all program calls" />
<meta name="abstract" content="When using programs compiled with either CLOSQLCSR(*ENDPGM) or CLOSQLCSR(*ENDMOD), a cursor must be opened every time the program or module is called, in order to access the data. If the SQL program or module is going to be called several times, and you want to take advantage of a reusable ODP, then the cursor must be explicitly closed before the program or module exits." />
<meta name="description" content="When using programs compiled with either CLOSQLCSR(*ENDPGM) or CLOSQLCSR(*ENDMOD), a cursor must be opened every time the program or module is called, in order to access the data. If the SQL program or module is going to be called several times, and you want to take advantage of a reusable ODP, then the cursor must be explicitly closed before the program or module exits." />
<meta name="DC.subject" content="rule, retaining cursor positions, program calls, all program calls, rules, rules for retaining cursor positions, using, close SQL cursor (CLOSQLCSR), CLOSQLCSR parameter, precompiler parameter, CLOSQLCSR" />
<meta name="keywords" content="rule, retaining cursor positions, program calls, all program calls, rules, rules for retaining cursor positions, using, close SQL cursor (CLOSQLCSR), CLOSQLCSR parameter, precompiler parameter, CLOSQLCSR" />
<meta name="DC.Relation" scheme="URI" content="retaincursors.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="retrs" />
<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>General rules for retaining cursor positions for all program calls</title>
</head>
<body id="retrs"><a name="retrs"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">General rules for retaining cursor positions for all program calls</h1>
<div><p>When using programs compiled with either CLOSQLCSR(*ENDPGM) or
CLOSQLCSR(*ENDMOD), a cursor must be opened every time the program or module
is called, in order to access the data. If the SQL program or module is going
to be called several times, and you want to take advantage of a reusable ODP,
then the cursor must be explicitly closed before the program or module exits.</p>
<div class="section"><p>Using the CLOSQLCSR parameter and specifying *ENDSQL, *ENDJOB,
or *ENDACTGRP, you may not need to run an OPEN and a CLOSE statement on every
call. In addition to having fewer statements to run, you can maintain the
cursor position between calls to the program or module.</p>
</div>
<div class="section"><p>The following examples of SQL statements help demonstrate the
advantage of using the CLOSQLCSR parameter: </p>
<pre> EXEC SQL
<strong>DECLARE</strong> DEPTDATA <strong>CURSOR FOR</strong>
<strong>SELECT</strong> EMPNO, LASTNAME
<strong>FROM</strong> CORPDATA.EMPLOYEE
<strong>WHERE</strong> WORKDEPT = :DEPTNUM
END-EXEC.
EXEC SQL
<strong>OPEN</strong> DEPTDATA
END-EXEC.
EXEC SQL
<strong>FETCH</strong> DEPTDATA <strong>INTO</strong> :EMPNUM, :LNAME
END-EXEC.
EXEC SQL
<strong>CLOSE</strong> DEPTDATA
END-EXEC.</pre>
<p>If this program is called several times from another
SQL program, it will be able to use a reusable ODP. This means that, as long
as SQL remains active between the calls to this program, the OPEN statement
will not require a database open operation. However, the cursor is still positioned
to the first result row after each OPEN statement, and the FETCH statement
will always return the first row.</p>
</div>
<div class="section"><p>In the following example, the CLOSE statement has been removed:
</p>
<pre> EXEC SQL
<strong>DECLARE</strong> DEPTDATA <strong>CURSOR FOR</strong>
<strong>SELECT</strong> EMPNO, LASTNAME
<strong>FROM</strong> CORPDATA.EMPLOYEE
<strong>WHERE</strong> WORKDEPT = :DEPTNUM
END-EXEC.
IF CURSOR-CLOSED IS = TRUE THEN
EXEC SQL
<strong>OPEN</strong> DEPTDATA
END-EXEC.
EXEC SQL
<strong>FETCH</strong> DEPTDATA <strong>INTO</strong> :EMPNUM, :LNAME
END-EXEC.</pre>
<p>If this program is precompiled with the *ENDJOB
option or the *ENDACTGRP option and the activation group remains active, the
cursor position is maintained. The cursor position is also maintained when
the following occurs:</p>
<ul><li>The program is precompiled with the *ENDSQL option.</li>
<li>SQL remains active between program calls.</li>
</ul>
</div>
<div class="section"><p>The result of this strategy is that each call to the program retrieves
the next row in the cursor. On subsequent data requests, the OPEN statement
is unnecessary and, in fact, fails with a -502 SQLCODE. You can ignore the
error, or add code to skip the OPEN. You can do this by using a FETCH statement
first, and then running the OPEN statement only if the FETCH operation failed.</p>
</div>
<div class="section"><p>This technique also applies to prepared statements. A program
can first try the EXECUTE, and if it fails, perform the PREPARE. The result
is that the PREPARE is only needed on the first call to the program, assuming
the correct CLOSQLCSR option was chosen. Of course, if the statement can change
between calls to the program, it should perform the PREPARE in all cases.</p>
</div>
<div class="section"><div class="p">The main program might also control this by sending a special
parameter on the first call only. This special parameter value indicates
that because it is the first call, the subprogram should perform the OPENs,
PREPAREs, and LOCKs. <div class="note"><span class="notetitle">Note:</span> If you are using COBOL programs, do not use the
STOP RUN statement. When the first COBOL program on the call stack ends or
a STOP RUN statement runs, a reclaim resource (RCLRSC) operation
is done. This operation closes the SQL cursor. The *ENDSQL option does not
work as you wanted.</div>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="retaincursors.htm" title="You can improve performance by retaining cursor positions.">Retain cursor positions</a></div>
</div>
</div>
</body>
</html>