211 lines
13 KiB
HTML
211 lines
13 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="Examples: Use a cursor" />
|
|||
|
<meta name="abstract" content="Suppose your program examines data about people in department D11. The following examples show the SQL statements you would include in a program to define and use a serial and a scrollable cursor." />
|
|||
|
<meta name="description" content="Suppose your program examines data about people in department D11. The following examples show the SQL statements you would include in a program to define and use a serial and a scrollable cursor." />
|
|||
|
<meta name="DC.subject" content="cursor, example overview, examples" />
|
|||
|
<meta name="keywords" content="cursor, example overview, examples" />
|
|||
|
<meta name="DC.Relation" scheme="URI" content="rbafycurs.htm" />
|
|||
|
<meta name="DC.Relation" scheme="URI" content="rbafycurs1.htm" />
|
|||
|
<meta name="DC.Relation" scheme="URI" content="rbafycurs2.htm" />
|
|||
|
<meta name="DC.Relation" scheme="URI" content="rbafycurs3.htm" />
|
|||
|
<meta name="DC.Relation" scheme="URI" content="rbafycurs4.htm" />
|
|||
|
<meta name="DC.Relation" scheme="URI" content="rbafycurs5a.htm" />
|
|||
|
<meta name="DC.Relation" scheme="URI" content="rbafycurs5b.htm" />
|
|||
|
<meta name="DC.Relation" scheme="URI" content="rbafycurs6.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="rbafycursorexamp" />
|
|||
|
<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>Examples: Use a cursor</title>
|
|||
|
</head>
|
|||
|
<body id="rbafycursorexamp"><a name="rbafycursorexamp"><!-- --></a>
|
|||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|||
|
<h1 class="topictitle1">Examples: Use a cursor</h1>
|
|||
|
<div><p>Suppose your program examines data about people in department D11.
|
|||
|
The following examples show the SQL statements you would include in a program
|
|||
|
to define and use a serial and a scrollable cursor.</p>
|
|||
|
<div class="section"><p>These cursors can be used to obtain information about the department
|
|||
|
from the CORPDATA.EMPLOYEE table.</p>
|
|||
|
</div>
|
|||
|
<div class="section"><p>For the serial cursor example, the program processes all of the
|
|||
|
rows from the table, updating the job for all members of department D11 and
|
|||
|
deleting the records of employees from the other departments.</p>
|
|||
|
</div>
|
|||
|
<div class="section"><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 class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" width="100%" frame="hsides" border="1" rules="rows"><caption>Table 1. A serial cursor example</caption><thead align="left"><tr><th align="left" valign="bottom" width="63.61323155216285%" id="d0e41">Serial cursor SQL statement</th>
|
|||
|
<th align="left" valign="bottom" width="36.38676844783715%" id="d0e43">Described in section</th>
|
|||
|
</tr>
|
|||
|
</thead>
|
|||
|
<tbody><tr><td align="left" valign="top" width="63.61323155216285%" headers="d0e41 "> <p>EXEC SQL<br />
|
|||
|
<strong>DECLARE</strong> THISEMP <strong>CURSOR FOR</strong><br />
|
|||
|
<strong>SELECT</strong> EMPNO, LASTNAME,<br />
|
|||
|
WORKDEPT, JOB<br />
|
|||
|
<strong>FROM</strong> CORPDATA.EMPLOYEE<br />
|
|||
|
<strong>FOR UPDATE OF</strong> JOB<br />
|
|||
|
END-EXEC.</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="36.38676844783715%" headers="d0e43 "><a href="rbafycurs1.htm#rbafycurs1">Step 1: Define the cursor</a>.</td>
|
|||
|
</tr>
|
|||
|
<tr><td align="left" valign="top" width="63.61323155216285%" headers="d0e41 "> <p>EXEC SQL<br />
|
|||
|
<strong>OPEN</strong> THISEMP<br />
|
|||
|
END-EXEC.</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="36.38676844783715%" headers="d0e43 "><a href="rbafycurs2.htm#rbafycurs2">Step 2: Open the cursor</a>.</td>
|
|||
|
</tr>
|
|||
|
<tr><td align="left" valign="top" width="63.61323155216285%" headers="d0e41 "> <p>EXEC SQL<br />
|
|||
|
<strong>WHENEVER NOT FOUND</strong><br />
|
|||
|
<strong>GO TO </strong>CLOSE-THISEMP<br />
|
|||
|
END-EXEC.</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="36.38676844783715%" headers="d0e43 "><a href="rbafycurs3.htm#rbafycurs3">Step 3: Specify what to do when end-of-data is reached</a>.</td>
|
|||
|
</tr>
|
|||
|
<tr><td align="left" valign="top" width="63.61323155216285%" headers="d0e41 "> <p>EXEC SQL<br />
|
|||
|
<strong>FETCH</strong> THISEMP<br />
|
|||
|
<strong>INTO</strong> :EMP-NUM, :NAME2,<br />
|
|||
|
:DEPT, :JOB-CODE<br />
|
|||
|
END-EXEC.</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="36.38676844783715%" headers="d0e43 "><a href="rbafycurs4.htm#rbafycurs4">Step 4: Retrieve a row using a cursor</a>.</td>
|
|||
|
</tr>
|
|||
|
<tr><td align="left" valign="top" width="63.61323155216285%" headers="d0e41 "> <p>... for all employees<br />
|
|||
|
in department D11, update<br />
|
|||
|
the JOB value:<br />
|
|||
|
<br />
|
|||
|
EXEC SQL<br />
|
|||
|
<strong>UPDATE</strong> CORPDATA.EMPLOYEE<br />
|
|||
|
<strong>SET</strong> JOB = :NEW-CODE<br />
|
|||
|
<strong>WHERE CURRENT OF</strong> THISEMP<br />
|
|||
|
END-EXEC.<br />
|
|||
|
<br />
|
|||
|
... then print the row.</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="36.38676844783715%" headers="d0e43 "><a href="rbafycurs5a.htm#rbafycurs5a">Step 5a: Update the current row</a>.</td>
|
|||
|
</tr>
|
|||
|
<tr><td align="left" valign="top" width="63.61323155216285%" headers="d0e41 "> <p>... for other employees,<br />
|
|||
|
delete the row:<br />
|
|||
|
<br />
|
|||
|
EXEC SQL<br />
|
|||
|
<strong>DELETE FROM</strong> CORPDATA.EMPLOYEE<br />
|
|||
|
<strong>WHERE CURRENT OF</strong> THISEMP<br />
|
|||
|
END-EXEC.</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="36.38676844783715%" headers="d0e43 "><a href="rbafycurs5b.htm#rbafycurs5b">Step 5b: Delete the current row</a>.</td>
|
|||
|
</tr>
|
|||
|
<tr><td align="left" valign="top" width="63.61323155216285%" headers="d0e41 ">Branch back to fetch and process the next
|
|||
|
row.</td>
|
|||
|
<td align="left" valign="top" width="36.38676844783715%" headers="d0e43 "> </td>
|
|||
|
</tr>
|
|||
|
<tr><td align="left" valign="top" width="63.61323155216285%" headers="d0e41 "> <p>CLOSE-THISEMP.<br />
|
|||
|
EXEC SQL<br />
|
|||
|
<strong>CLOSE</strong> THISEMP<br />
|
|||
|
END-EXEC.</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="36.38676844783715%" headers="d0e43 "><a href="rbafycurs6.htm#rbafycurs6">Step 6: Close the cursor</a>.</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
<div class="section"><p>For the scrollable cursor example, the program uses the RELATIVE
|
|||
|
position option to obtain a representative sample of salaries from department
|
|||
|
D11.</p>
|
|||
|
</div>
|
|||
|
|
|||
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" width="100%" frame="hsides" border="1" rules="rows"><caption>Table 2. Scrollable cursor example</caption><thead align="left"><tr><th align="left" valign="bottom" width="55.69620253164557%" id="d0e173">Scrollable cursor SQL statement</th>
|
|||
|
<th align="left" valign="bottom" width="44.303797468354425%" id="d0e175">Described in section</th>
|
|||
|
</tr>
|
|||
|
</thead>
|
|||
|
<tbody><tr><td align="left" valign="top" width="55.69620253164557%" headers="d0e173 "> <p>EXEC SQL<br />
|
|||
|
<strong>DECLARE</strong> THISEMP <strong>DYNAMIC SCROLL CURSOR FOR</strong><br />
|
|||
|
<strong>SELECT</strong> EMPNO, LASTNAME,<br />
|
|||
|
SALARY<br />
|
|||
|
<strong>FROM</strong> CORPDATA.EMPLOYEE<br />
|
|||
|
<strong>WHERE</strong> WORKDEPT = 'D11'<br />
|
|||
|
END-EXEC.</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="44.303797468354425%" headers="d0e175 "><a href="rbafycurs1.htm#rbafycurs1">Step 1: Define the cursor</a>.</td>
|
|||
|
</tr>
|
|||
|
<tr><td align="left" valign="top" width="55.69620253164557%" headers="d0e173 "> <p>EXEC SQL<br />
|
|||
|
<strong>OPEN</strong> THISEMP<br />
|
|||
|
END-EXEC.</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="44.303797468354425%" headers="d0e175 "><a href="rbafycurs2.htm#rbafycurs2">Step 2: Open the cursor</a>.</td>
|
|||
|
</tr>
|
|||
|
<tr><td align="left" valign="top" width="55.69620253164557%" headers="d0e173 "> <p>EXEC SQL<br />
|
|||
|
<strong>WHENEVER NOT FOUND</strong><br />
|
|||
|
<strong>GO TO</strong> CLOSE-THISEMP<br />
|
|||
|
END-EXEC.</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="44.303797468354425%" headers="d0e175 "><a href="rbafycurs3.htm#rbafycurs3">Step 3: Specify what to do when end-of-data is reached</a>.</td>
|
|||
|
</tr>
|
|||
|
<tr><td align="left" valign="top" width="55.69620253164557%" headers="d0e173 "> <p>...initialize program summation<br />
|
|||
|
salary variable<br />
|
|||
|
EXEC SQL<br />
|
|||
|
<strong>FETCH RELATIVE</strong> 3 <strong>FROM</strong> THISEMP<br />
|
|||
|
<strong>INTO</strong> :EMP-NUM, :NAME2,<br />
|
|||
|
:JOB-CODE<br />
|
|||
|
END-EXEC.<br />
|
|||
|
...add the current salary to<br />
|
|||
|
program summation salary<br />
|
|||
|
...branch back to fetch and<br />
|
|||
|
process the next row.</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="44.303797468354425%" headers="d0e175 "><a href="rbafycurs4.htm#rbafycurs4">Step 4: Retrieve a row using a cursor</a>.</td>
|
|||
|
</tr>
|
|||
|
<tr><td align="left" valign="top" width="55.69620253164557%" headers="d0e173 "> <p>...calculate the average<br />
|
|||
|
salary</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="44.303797468354425%" headers="d0e175 "> </td>
|
|||
|
</tr>
|
|||
|
<tr><td align="left" valign="top" width="55.69620253164557%" headers="d0e173 "> <p>CLOSE-THISEMP.<br />
|
|||
|
EXEC SQL<br />
|
|||
|
<strong>CLOSE</strong> THISEMP<br />
|
|||
|
END-EXEC.</p>
|
|||
|
</td>
|
|||
|
<td align="left" valign="top" width="44.303797468354425%" headers="d0e175 "><a href="rbafycurs6.htm#rbafycurs6">Step 6: Close the cursor</a>.</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div>
|
|||
|
<ul class="ullinks">
|
|||
|
<li class="ulchildlink"><strong><a href="rbafycurs1.htm">Step 1: Define the cursor</a></strong><br />
|
|||
|
To define a result table to be accessed with a cursor, use the DECLARE CURSOR statement.</li>
|
|||
|
<li class="ulchildlink"><strong><a href="rbafycurs2.htm">Step 2: Open the cursor</a></strong><br />
|
|||
|
To begin processing the rows of the result table, issue the OPEN statement.</li>
|
|||
|
<li class="ulchildlink"><strong><a href="rbafycurs3.htm">Step 3: Specify what to do when end-of-data is reached</a></strong><br />
|
|||
|
To find out when the end of the result table is reached, test the SQLCODE field for a value of 100 or test the SQLSTATE field for a value of '02000' (that is, end-of-data). This condition occurs when the FETCH statement has retrieved the last row in the result table and your program issues a subsequent FETCH.</li>
|
|||
|
<li class="ulchildlink"><strong><a href="rbafycurs4.htm">Step 4: Retrieve a row using a cursor</a></strong><br />
|
|||
|
To move the contents of a selected row into your program's host variables, use the FETCH statement. The SELECT statement within the DECLARE CURSOR statement identifies rows that contain the column values your program wants. However, SQL does not retrieve any data for your application program until the FETCH statement is issued.</li>
|
|||
|
<li class="ulchildlink"><strong><a href="rbafycurs5a.htm">Step 5a: Update the current row</a></strong><br />
|
|||
|
When your program has positioned the cursor on a row, you can update its data by using the UPDATE statement with the WHERE CURRENT OF clause. The WHERE CURRENT OF clause specifies a cursor that points to the row you want to update.</li>
|
|||
|
<li class="ulchildlink"><strong><a href="rbafycurs5b.htm">Step 5b: Delete the current row</a></strong><br />
|
|||
|
When your program has retrieved the current row, you can delete the row by using the DELETE statement. To do this, you issue a DELETE statement designed for use with a cursor; the WHERE CURRENT OF clause specifies a cursor that points to the row you want to delete.</li>
|
|||
|
<li class="ulchildlink"><strong><a href="rbafycurs6.htm">Step 6: Close the cursor</a></strong><br />
|
|||
|
If you processed the rows of a result table for a serial cursor, and you want to use the cursor again, issue a CLOSE statement to close the cursor prior to re-opening it.</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<div class="familylinks">
|
|||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafycurs.htm" title="When SQL runs a select statement, the resulting rows comprise the result table. A cursor provides a way to access a result table.">Use a cursor</a></div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</body>
|
|||
|
</html>
|