ibm-information-center/dist/eclipse/plugins/i5OS.ic.sqlp_5.4.0.1/rbafymultifetch1.htm

140 lines
7.0 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="Multiple-row FETCH using a host structure array" />
<meta name="abstract" content="To use the multiple-row FETCH with the host structure array, the application must define a host structure array that can be used by SQL." />
<meta name="description" content="To use the multiple-row FETCH with the host structure array, the application must define a host structure array that can be used by SQL." />
<meta name="DC.subject" content="FETCH statement, using host structure array, statements, FETCH" />
<meta name="keywords" content="FETCH statement, using host structure array, statements, FETCH" />
<meta name="DC.Relation" scheme="URI" content="rbafymrfetch.htm" />
<meta name="DC.Relation" scheme="URI" content="../db2/rbafzmstsqlcca.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="rbafymultifetch1" />
<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>Multiple-row FETCH using a host structure array</title>
</head>
<body id="rbafymultifetch1"><a name="rbafymultifetch1"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Multiple-row FETCH using a host structure array</h1>
<div><p>To use the multiple-row FETCH with the host structure array, the
application must define a host structure array that can be used by SQL.</p>
<div class="section"><p>Each language has its own conventions and rules for defining a
host structure array. Host structure arrays can be defined by using variable
declarations or by using compiler directives to retrieve External File Descriptions
(such as the COBOL COPY directive).</p>
</div>
<div class="section"><p>The host structure array consists of an array of structures. Each
structure corresponds to one row of the result table. The first structure
in the array corresponds to the first row, the second structure in the array
corresponds to the second row, and so on. SQL determines the attributes of
elementary items in the host structure array based on the declaration of the
host structure array. To maximize performance, the attributes of the items
that make up the host structure array should match the attributes of the columns
being retrieved.</p>
</div>
<div class="section"><p>Consider the following COBOL example:</p>
<div class="p"><div class="note"><span class="notetitle">Note:</span> By using the
code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
</div>
<pre>
EXEC SQL <strong>INCLUDE SQLCA</strong>
END-EXEC.
01 TABLE-1.
02 DEPT OCCURS 10 TIMES.
05 EMPNO PIC X(6).
05 LASTNAME.
49 LASTNAME-LEN PIC S9(4) BINARY.
49 LASTNAME-TEXT PIC X(15).
05 WORKDEPT PIC X(3).
05 JOB PIC X(8).
01 TABLE-2.
02 IND-ARRAY OCCURS 10 TIMES.
05 INDS PIC S9(4) BINARY OCCURS 4 TIMES.
EXEC SQL
<strong>DECLARE</strong> D11 <strong>CURSOR FOR</strong>
<strong>SELECT</strong> EMPNO, LASTNAME, WORKDEPT, JOB
<strong>FROM</strong> CORPDATA.EMPLOYEE
<strong>WHERE</strong> WORKDEPT = "D11"
END-EXEC.
EXEC SQL
<strong>OPEN</strong> D11
END-EXEC.
PERFORM FETCH-PARA UNTIL SQLCODE NOT EQUAL TO ZERO.
ALL-DONE.
EXEC SQL <strong>CLOSE</strong> D11 END-EXEC.
FETCH-PARA.
EXEC SQL <strong>WHENEVER NOT FOUND GO TO</strong> ALL-DONE END-EXEC.
EXEC SQL <strong>FETCH</strong> D11 <strong>FOR</strong> 10 <strong>ROWS INTO</strong> :DEPT :IND-ARRAY
END-EXEC.
</pre>
</div>
<div class="section"><p>In this example, a cursor was defined for the CORPDATA.EMPLOYEE
table to select all rows where the WORKDEPT column equals 'D11'. The result
table contains eight rows. The DECLARE CURSOR and OPEN statements do not have
any special syntax when they are used with a multiple-row FETCH statement.
Another FETCH statement that returns a single row against the same cursor
can be coded elsewhere in the program. The multiple-row FETCH statement is
used to retrieve all of the rows in the result table. Following the FETCH,
the cursor position remains on the last row retrieved.</p>
</div>
<div class="section"><p>The host structure array DEPT and the associated indicator array
IND-ARRAY are defined in the application. Both arrays have a dimension of
ten. The indicator array has an entry for each column in the result table.</p>
</div>
<div class="section"><p>The attributes of type and length of the DEPT host structure array
elementary items match the columns that are being retrieved.</p>
</div>
<div class="section"><p>When the multiple-row FETCH statement has successfully completed,
the host structure array contains the data for all eight rows. The indicator
array, IND_ARRAY, contains zeros for every column in every row because no
NULL values were returned.</p>
</div>
<div class="section"><p>The SQLCA that is returned to the application contains the following
information:</p>
<ul><li>SQLCODE contains 0</li>
<li>SQLSTATE contains '00000'</li>
<li>SQLERRD3 contains 8, the number of rows fetched</li>
<li>SQLERRD4 contains 34, the length of each row</li>
<li>SQLERRD5 contains +100, indicating the last row in the result table is
in the block</li>
</ul>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafymrfetch.htm" title="The multiple-row FETCH statement can be used to retrieve multiple rows from a table or view with a single FETCH. The program controls the blocking of rows by the number of rows requested on the FETCH statement (OVRDBF has no effect).">Use the multiple-row FETCH statement</a></div>
</div>
<div class="relinfo"><strong>Related information</strong><br />
<div><a href="../db2/rbafzmstsqlcca.htm">SQLCA (SQL communications area)</a></div>
</div>
</div>
</body>
</html>