0% found this document useful (0 votes)
98 views5 pages

Using The Multiple-Row FETCH Statement: Step 1:declare A Cursor

The document discusses the steps to retrieve and manipulate data from a database using a cursor in SQL. It describes: 1) Declaring a cursor to define the result table and select statement. 2) Opening the cursor to identify rows in the result table. 3) Retrieving rows from the result table using FETCH and testing for end of data. 4) Updating or deleting the current row using the WHERE CURRENT OF clause. 5) Closing the cursor when finished.

Uploaded by

nairarung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views5 pages

Using The Multiple-Row FETCH Statement: Step 1:declare A Cursor

The document discusses the steps to retrieve and manipulate data from a database using a cursor in SQL. It describes: 1) Declaring a cursor to define the result table and select statement. 2) Opening the cursor to identify rows in the result table. 3) Retrieving rows from the result table using FETCH and testing for end of data. 4) Updating or deleting the current row using the WHERE CURRENT OF clause. 5) Closing the cursor when finished.

Uploaded by

nairarung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Using the multiple-row FETCH statement

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). The maximum
number of rows that can be requested on a single fetch call is 32767. Once the data is
retrieved, the cursor is positioned on the last row retrieved.
The multiple-row FETCH statement can be used with both serial and scrollable cursors.
The operations used to define, open, and close a cursor for a multiple-row FETCH remain
the same. Only the FETCH statement changes to specify the number of rows to retrieve
and the storage where the rows are placed.
After each multiple-row FETCH, information is returned to the program through the
SQLCA. In addition to the SQLCODE and SQLSTATE fields, the SQLERRD provides
the following information:

SQLERRD3 contains the number of rows retrieved on the multiple-row FETCH


statement. If SQLERRD3 is less than the number of rows requested, then an error
or end-of-data condition occurred.

Step 1 :Declare a cursor


To define a result table to be accessed with a cursor, use the DECLARE CURSOR
statement.
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):
EXEC SQL
DECLARE cursor-name CURSOR FOR
SELECT column-1, column-2 ,...
FROM table-name , ...
FOR UPDATE OF column-2 ,...
END-EXEC.
For Scrollable Cursor
For a scrollable cursor, the statement looks like this (the WHERE clause
is optional):
EXEC SQL
DECLARE cursor-name DYNAMIC SCROLL CURSOR FOR
SELECT column-1, column-2 ,...
FROM table-name ,...
WHERE column-1 = expression ...
END-EXEC.

Other Clauses

FOR UPDATE OF clause


negative SQLCODE is returned if an update is attempted
ORDER BY clause
FOR READ ONLY
Step 2:Open the cursor
To begin processing the rows of the result table, issue the OPEN statement. When your
program issues the OPEN statement, SQL processes the select-statement within the
DECLARE CURSOR statement to identify a set of rows, called a result table , using the
current value of any host variables specified in the select-statement. A result table can
contain zero, one, or many rows, depending on the extent to which the search condition is
satisfied. The OPEN statement looks like this:
EXEC SQL
OPEN cursor-name
END-EXEC.

Step 3: Specify what to do when end-of-data is reached


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. For example:
IF SQLCODE =100 GO TO DATA-NOT-FOUND.
or
IF SQLSTATE ='02000' GO TO DATA-NOT-FOUND.
Step 4: Retrieve a row using a cursor

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.
The serial cursor FETCH statement looks like this:
EXEC SQL
FETCH cursor-name
INTO :host variable-1[, :host variable-2] ...
END-EXEC.

The scrollable cursor FETCH statement looks like this:


EXEC SQL
FETCH RELATIVE integer
FROM cursor-name
INTO :host variable-1[, :host variable-2] ...
END-EXEC.

Step 5a: Update the current row

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. The
UPDATE ... WHERE CURRENT OF statement looks like this:
EXEC SQL
UPDATE table-name
SET column-1 = value [, column-2 = value] ...
WHERE CURRENT OF cursor-name
END-EXEC.
Step 5b: Delete the current row

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. The DELETE ... WHERE CURRENT OF statement looks like this:
EXEC SQL
DELETE FROM table-name
WHERE CURRENT OF cursor-name
END-EXEC.
Step 6: Close the Cursor

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.
EXEC SQL
CLOSE cursor-name
END-EXEC.

If you processed the rows of a result table and you do not want to use the cursor again,
you can let the system close the cursor. The system automatically closes the cursor when:

A COMMIT without HOLD statement is issued and the cursor is not declared
using the WITH HOLD clause.

A ROLLBACK without HOLD statement is issued.


The job ends.
The activation group ends and CLOSQLCSR(*ENDACTGRP) was specified on
the precompile.
The first SQL program in the call stack ends and neither
CLOSQLCSR(*ENDJOB) or CLOSQLCSR(*ENDACTGRP) was specified
when the program was precompiled.
The connection to the application server is ended using the DISCONNECT
statement.
The connection to the application server was released and a successful COMMIT
occurred.
An *RUW CONNECT occurred.
Because an open cursor still holds locks on referred-to-tables or views, you
should explicitly close any open cursors as soon as they are no longer needed.

Dynamic Cursor
To issue a dynamic SQL statement, you must use the statement with either an EXECUTE
statement or an EXECUTE IMMEDIATE statement, because dynamic SQL statements
are not prepared at precompile time and therefore must be prepared at run time. The
EXECUTE IMMEDIATE statement causes the SQL statement to be prepared and run
dynamically at program run time.
There are two basic types of dynamic SQL statements: SELECT statements and nonSELECT statements. Non-SELECT statements include such statements as DELETE,
INSERT, and UPDATE.

To build a dynamic SQL non-SELECT statement:


1. Verify that the SQL statement you want to build is one that can be run
dynamically.
2. Build the SQL statement. (Use Interactive SQL for an easy way to build, verify,
and run your SQL statement.)
To run a dynamic SQL non-SELECT statement:

1. Run the SQL statement using EXECUTE IMMEDIATE, or PREPARE the SQL
statement, then EXECUTE the prepared statement.
2. Handle any SQL return codes that might result.
The following is an example of an application running a dynamic SQL non-SELECT
statement (stmtstrg):
EXEC SQL
EXECUTE IMMEDIATE: stmtstrg;
Using the PREPARE and EXECUTE statements
If non-SELECT statements contain no parameter markers, they can be run
dynamically using the EXECUTE IMMEDIATE statement. However, if the nonSELECT statements have parameter markers, they must be run using PREPARE
and EXECUTE.

You might also like