110 lines
6.6 KiB
HTML
110 lines
6.6 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="Step 1: Define the cursor" />
|
||
<meta name="abstract" content="To define a result table to be accessed with a cursor, use the DECLARE CURSOR statement." />
|
||
<meta name="description" content="To define a result table to be accessed with a cursor, use the DECLARE CURSOR statement." />
|
||
<meta name="DC.subject" content="cursor, defining a cursor, example, examples, column, FOR UPDATE OF clause, FOR UPDATE OF clause, restrictions, read-only, table" />
|
||
<meta name="keywords" content="cursor, defining a cursor, example, examples, column, FOR UPDATE OF clause, FOR UPDATE OF clause, restrictions, read-only, table" />
|
||
<meta name="DC.Relation" scheme="URI" content="rbafycursorexamp.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="rbafycurs1" />
|
||
<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>Step 1: Define the cursor</title>
|
||
</head>
|
||
<body id="rbafycurs1"><a name="rbafycurs1"><!-- --></a>
|
||
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
<h1 class="topictitle1">Step 1: Define the cursor</h1>
|
||
<div><p>To define a result table to be accessed with a cursor, use the
|
||
DECLARE CURSOR statement.</p>
|
||
<div class="section"><p>The DECLARE CURSOR statement names a cursor and specifies a select-statement.
|
||
The select-statement defines a set of rows that, conceptually, make up the
|
||
result table. For a serial cursor, the statement looks like this (the FOR
|
||
UPDATE OF clause is optional):</p>
|
||
<pre>EXEC SQL
|
||
<strong>DECLARE</strong> cursor-name <strong>CURSOR FOR</strong>
|
||
<strong>SELECT</strong> column-1, column-2 ,...
|
||
<strong>FROM</strong> table-name , ...
|
||
<strong>FOR UPDATE OF</strong> column-2 ,...
|
||
END-EXEC.</pre>
|
||
<p> For a scrollable cursor, the statement looks like
|
||
this (the WHERE clause is optional):</p>
|
||
<pre>EXEC SQL
|
||
<strong>DECLARE</strong> cursor-name <strong> SCROLL CURSOR FOR</strong>
|
||
<strong>SELECT</strong> column-1, column-2 ,...
|
||
<strong>FROM</strong> table-name ,...
|
||
<strong>WHERE</strong> column-1 = expression ...
|
||
END-EXEC.</pre>
|
||
</div>
|
||
<div class="section"><p>The select-statements shown here are rather simple. However, you
|
||
can code several other types of clauses in a select-statement within a DECLARE
|
||
CURSOR statement for a serial and a scrollable cursor.</p>
|
||
</div>
|
||
<div class="section"><p>If you intend to update any columns in any or all of the rows
|
||
of the identified table (the table named in the FROM clause), include the
|
||
FOR UPDATE OF clause. It names each column you intend to update. If you do
|
||
not specify the names of columns, and you specify either the ORDER BY clause
|
||
or FOR READ ONLY clause, a negative SQLCODE is returned if an update is attempted.
|
||
If you do not specify the FOR UPDATE OF clause, the FOR READ ONLY clause,
|
||
the ORDER BY clause, and the result table is not read-only and the cursor
|
||
is not scrollable, you can update any of the columns of the specified table.</p>
|
||
</div>
|
||
<div class="section"><p>You can update a column of the identified table even though it
|
||
is not part of the result table. In this case, you do not need to name the
|
||
column in the SELECT statement. When the cursor retrieves a row (using FETCH)
|
||
that contains a column value you want to update, you can use UPDATE ... WHERE
|
||
CURRENT OF to update the row.</p>
|
||
</div>
|
||
<div class="section"><p>For example, assume that each row of the result table includes
|
||
the <em>EMPNO</em>, <em>LASTNAME</em>, and <em>WORKDEPT</em> columns from the CORPDATA.EMPLOYEE
|
||
table. If you want to update the <em>JOB</em> column (one of the columns in
|
||
each row of the CORPDATA.EMPLOYEE table), the DECLARE CURSOR statement should
|
||
include FOR UPDATE OF JOB ... even though JOB is omitted from the SELECT statement.</p>
|
||
</div>
|
||
<div class="section"><p>The result table and cursor are <em>read-only</em> if any of the
|
||
following are true:</p>
|
||
<ul><li>The first FROM clause identifies more than one table or view.</li>
|
||
<li>The first FROM clause identifies a read-only view.</li>
|
||
<li>The first FROM clause identifies a user-defined table function.</li>
|
||
<li>The first SELECT clause specifies the keyword DISTINCT.</li>
|
||
<li>The outer subselect contains a GROUP BY clause.</li>
|
||
<li>The outer subselect contains a HAVING clause.</li>
|
||
<li>The first SELECT clause contains a column function.</li>
|
||
<li>The select-statement contains a subquery such that the base object of
|
||
the outer subselect and of the subquery is the same table.</li>
|
||
<li><img src="./delta.gif" alt="Start of change" />The select-statement contains a UNION, UNION ALL, EXCEPT,
|
||
or INTERSECT operator.<img src="./deltaend.gif" alt="End of change" /></li>
|
||
<li>The select-statement contains an ORDER BY clause, and the SENSITIVE keyword
|
||
and FOR UPDATE OF clause are not specified.</li>
|
||
<li>The select-statement includes a FOR READ ONLY clause.</li>
|
||
<li>The SCROLL keyword is specified, a FOR UPDATE OF clause is not specified,
|
||
and the SENSITIVE keyword is not specified.</li>
|
||
<li>The select-list includes a DataLink column and a FOR UPDATE OF clause
|
||
is not specified.</li>
|
||
<li>The first subselect requires a temporary result table.</li>
|
||
<li>The select-statement includes a FETCH FIRST <em>n</em> ROWS ONLY.</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<div class="familylinks">
|
||
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafycursorexamp.htm" title="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.">Examples: Use a cursor</a></div>
|
||
</div>
|
||
</div>
|
||
</body>
|
||
</html> |