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

183 lines
9.2 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 row storage area" />
<meta name="abstract" content="The application must define a row storage area and an associated description area before the application can use a multiple-row FETCH with a row storage area. The row storage area is a host variable defined in the application program." />
<meta name="description" content="The application must define a row storage area and an associated description area before the application can use a multiple-row FETCH with a row storage area. The row storage area is a host variable defined in the application program." />
<meta name="DC.subject" content="FETCH statement, using row storage area, using descriptor area, statements, FETCH" />
<meta name="keywords" content="FETCH statement, using row storage area, using descriptor area, statements, FETCH" />
<meta name="DC.Relation" scheme="URI" content="rbafymrfetch.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafysamptblx.htm" />
<meta name="DC.Relation" scheme="URI" content="../db2/rbafzmstsqldda.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="rbafymultifetch2" />
<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 row storage area</title>
</head>
<body id="rbafymultifetch2"><a name="rbafymultifetch2"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Multiple-row FETCH using a row storage area</h1>
<div><p>The application must define a row storage area and an associated
description area before the application can use a multiple-row FETCH with
a row storage area. The row storage area is a host variable defined in the
application program.</p>
<div class="section"><p>The row storage area contains the results of the multiple-row
FETCH. A row storage area can be a character variable with enough bytes to
hold all of the rows requested on the multiple-row FETCH.</p>
</div>
<div class="section"><p>An SQLDA that contains the SQLTYPE and SQLLEN for each returned
column is defined by the associated descriptor used on the row storage area
form of the multiple-row FETCH. The information provided in the descriptor
determines the data mapping from the database to the row storage area. To
maximize performance, the attribute information in the descriptor should match
the attributes of the columns retrieved.</p>
</div>
<div class="section"><p>Consider the following PL/I 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>
</div>
<div class="example"> <pre> *....+....1....+....2....+....3....+....4....+....5....+....6....+....7...*
EXEC SQL <strong>INCLUDE SQLCA</strong>;
EXEC SQL <strong>INCLUDE SQLDA</strong>;
DCL DEPTPTR PTR;
DCL 1 DEPT(20) BASED(DEPTPTR),
3 EMPNO CHAR(6),
3 LASTNAME CHAR(15) VARYING,
3 WORKDEPT CHAR(3),
3 JOB CHAR(8);
DCL I BIN(31) FIXED;
DEC J BIN(31) FIXED;
DCL ROWAREA CHAR(2000);
ALLOCATE SQLDA SET(SQLDAPTR);
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';
</pre>
<pre>
EXEC SQL
<strong>OPEN</strong> D11;
/* SET UP THE DESCRIPTOR FOR THE MULTIPLE-ROW FETCH */
/* 4 COLUMNS ARE BEING FETCHED */
SQLD = 4;
SQLN = 4;
SQLDABC = 366;
SQLTYPE(1) = 452; /* FIXED LENGTH CHARACTER - */
/* NOT NULLABLE */
SQLLEN(1) = 6;
SQLTYPE(2) = 456; /*VARYING LENGTH CHARACTER */
/* NOT NULLABLE */
SQLLEN(2) = 15;
SQLTYPE(3) = 452; /* FIXED LENGTH CHARACTER - */
SQLLEN(3) = 3;
SQLTYPE(4) = 452; /* FIXED LENGTH CHARACTER - */
/* NOT NULLABLE */
SQLLEN(4) = 8;
/*ISSUE THE MULTIPLE-ROW FETCH STATEMENT TO RETRIEVE*/
/*THE DATA INTO THE DEPT ROW STORAGE AREA */
/*USE A HOST VARIABLE TO CONTAIN THE COUNT OF */
/*ROWS TO BE RETURNED ON THE MULTIPLE-ROW FETCH */
J = 20; /*REQUESTS 20 ROWS ON THE FETCH */
EXEC SQL
<strong>WHENEVER NOT FOUND</strong>
<strong>GOTO</strong> FINISHED;
EXEC SQL
<strong>WHENEVER SQLERROR</strong>
<strong>GOTO</strong> FINISHED;
EXEC SQL
<strong>FETCH</strong> D11 <strong>FOR</strong> :J <strong>ROWS</strong>
<strong>USING DESCRIPTOR</strong> :SQLDA <strong>INTO</strong> :ROWAREA;
/* ADDRESS THE ROWS RETURNED */
DEPTPTR = ADDR(ROWAREA);
/*PROCESS EACH ROW RETURNED IN THE ROW STORAGE */
/*AREA BASED ON THE COUNT OF RECORDS RETURNED */
/*IN SQLERRD3. */
DO I = 1 TO SQLERRD(3);
IF EMPNO(I) = '000170' THEN
DO;
:
END;
END;
IF SQLERRD(5) = 100 THEN
DO;
/* PROCESS END OF FILE */
END;
FINISHED:
</pre>
</div>
<div class="section"><p>In this example, a cursor has been defined for the CORPDATA.EMPLOYEE
table to select all rows where the WORKDEPT column equal 'D11'. The sample
EMPLOYEE table in the Sample Tables shows the result table contains multiple
rows. The DECLARE CURSOR and OPEN statements do not have 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 rows
in the result table. Following the FETCH, the cursor position remains on the
final row in the block.</p>
</div>
<div class="section"><p>The row area, ROWAREA, is defined as a character array. The data
from the result table is placed in the host variable. In this example, a pointer
variable is assigned to the address of ROWAREA. Each item in the rows that
are returned is examined and used with the based structure DEPT.</p>
</div>
<div class="section"><p>The attributes (type and length) of the items in the descriptor
match the columns that are retrieved. In this case, no indicator area is provided.</p>
</div>
<div class="section"><p>After the FETCH statement is completed, the ROWAREA contains all
of the rows that equal 'D11', in this case 11 rows. The SQLCA that is returned
to the application contains the following:</p>
<ul><li>SQLCODE contains 0</li>
<li>SQLSTATE contains '00000'</li>
<li>SQLERRD3 contains 11, the number of rows returned</li>
<li>SQLERRD4 contains 34, for the length of the row fetched</li>
<li>SQLERRD5 contains +100, indicating the last row in the result table was
fetched</li>
</ul>
</div>
<div class="section"><p>In this example, the application has taken advantage of the fact
that SQLERRD5 contains an indication of the end of the file being reached.
As a result, the application does not need to call SQL again to attempt to
retrieve more rows. If the cursor has immediate sensitivity to inserts, you
should call SQL in case any records were added. Cursors have immediate sensitivity
when the commitment control level is something other than *RR.</p>
</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="relref"><strong>Related reference</strong><br />
<div><a href="rbafysamptblx.htm" title="This topic contains the sample tables referred to and used in this topic and the SQL Reference topic collection.">DB2 UDB for iSeries sample tables</a></div>
</div>
<div class="relinfo"><strong>Related information</strong><br />
<div><a href="../db2/rbafzmstsqldda.htm">Appendix D. SQLDA (SQL Descriptor Area)</a></div>
</div>
</div>
</body>
</html>