declare prepare fetch
TRANSCRIPT
-
8/10/2019 Declare Prepare Fetch
1/5
Using the multiple-row FETCH statement
The multiple-row FETCH statement can be used to retrieve multiple rows from a table orview with a single FETCH. The program controls the blocking of rows by the number of
rows requested on the FETCH statement !"#$%F has no effect&. The ma'imumnumber of rows that can be requested on a single fetch call is ()*+*. !nce 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. !nly the FETCH statement changes to specify the number of rows to retrieveand the storage where the rows are placed.
fter each multiple-row FETCH, information is returned to the program through the
/0C. 1n addition to the /0C!$E and /0TTE fields, the /0E##$ provides
the following information2
/0E##$( contains the number of rows retrieved on the multiple-row FETCH
statement. 1f /0E##$( 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 $EC0#E C3#!#
statement.
The $EC0#E C3#!# 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 aserial cursor, the statement looks like this the F!# 34$TE !F clause is optional&2
EXEC SQL
DECLAREcursor-name CURSOR FOR
SELECTcolumn-1, column-2 ,...
FROMtable-name , ...
FOR UPDATE OFcolumn-2 ,...
END-EXEC.
For Scrollable CursorFor a scrollable cursor, the statement looks like this (the !E"E clause
is o#tional$%
EXEC SQL
DECLAREcursor-name DYNAMIC SCROLL CURSOR FOR
SELECTcolumn-1, column-2 ,...
FROMtable-name ,...
WHEREcolumn-1 & e'#ression ...
END-EXEC.
http://publib.boulder.ibm.com/iseries/v5r1/ic2924/info/sqlp/rbafymst02.htm#ToC_117http://publib.boulder.ibm.com/iseries/v5r1/ic2924/info/sqlp/rbafymst02.htm#ToC_117 -
8/10/2019 Declare Prepare Fetch
2/5
-
8/10/2019 Declare Prepare Fetch
3/5
The scrollable cursor FETCH statement looks like this2
EXEC SQL
FETCH RELATI&Einte3er
FROMcursor-name
INTO%host 0ariable-1, %host 0ariable-2 ...
END-EXEC.
Step 'a: Up(ate the %urre#t r!"
7hen your program has positioned the cursor on a row, you can update its data by usingthe 34$TE statement with the 7HE#E C3##E6T !F clause. The 7HE#E
C3##E6T !F clause specifies a cursor that points to the row you want to update. The
34$TE ... 7HE#E C3##E6T !F statement looks like this2
EXEC SQL
UPDATEtable-name
SETcolumn-1 & 0alue , column-2 & 0alue ... WHERE CURRENT OFcursor-name
END-EXEC.
Step '): Delete the %urre#t r!"
7hen your program has retrieved the current row, you can delete the row by using the
$E0ETE statement. To do this, you issue a $E0ETE statement designed for use with acursor< the 7HE#E C3##E6T !F clause specifies a cursor that points to the row you
want to delete. The $E0ETE ... 7HE#E C3##E6T !F statement looks like this2
EXEC SQLDELETE FROMtable-name
WHERE CURRENT OFcursor-name
END-EXEC.
Step *: Cl!se the Curs!r
1f you processed the rows of a result table for a serial cursor, and you want to use the
cursor again, issue a C0!E statement to close the cursor prior to re-opening it.
EXEC SQL
CLOSEcursor-name
END-EXEC.
1f 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 when2
C!==1T without H!0$ statement is issued and the cursor is not declared
using the 71TH H!0$ clause.
http://publib.boulder.ibm.com/iseries/v5r1/ic2924/info/sqlp/rbafymst02.htm#ToC_114http://publib.boulder.ibm.com/iseries/v5r1/ic2924/info/sqlp/rbafymst02.htm#ToC_115http://publib.boulder.ibm.com/iseries/v5r1/ic2924/info/sqlp/rbafymst02.htm#ToC_114http://publib.boulder.ibm.com/iseries/v5r1/ic2924/info/sqlp/rbafymst02.htm#ToC_115 -
8/10/2019 Declare Prepare Fetch
4/5
#!00%C> without H!0$ statement is issued.
The ?ob ends.
The activation group ends and C0!/0C#@E6$CTA#4& was specified on
the precompile.
The first /0 program in the call stack ends and neither
C0!/0C#@E6$B!%& or C0!/0C#@E6$CTA#4& was specifiedwhen the program was precompiled.
The connection to the application server is ended using the $1C!66ECT
statement.
The connection to the application server was released and a successful C!==1T
occurred.
n @#37 C!66ECT occurred.
%ecause an open cursor still holds locks on referred-to-tables or views, you
should e'plicitly close any open cursors as soon as they are no longer needed.
D!namic Cursor
To issue a dynamic /0 statement, you must use the statement with either an EEC3TE
statement or an EEC3TE 1==E$1TE statement, because dynamic /0 statementsare not prepared at precompile time and therefore must be prepared at run time. The
EEC3TE 1==E$1TE statement causes the /0 statement to be prepared and rundynamically at program run time.
There are two basic types of dynamic /0 statements2 E0ECT statements and non-E0ECT statements. 6on-E0ECT statements include such statements as $E0ETE,
16E#T, and 34$TE.
To #uil" a "!namic S$% non-SE%ECT statement:
9. "erify that the /0 statement you want to build is one that can be rundynamically.
). %uild the /0 statement. 3se 1nteractive /0 for an easy way to build, verify,
and run your /0 statement.&
To run a dynamic /0 non-E0ECT statement2
-
8/10/2019 Declare Prepare Fetch
5/5
9. #un the /0 statement using EEC3TE 1==E$1TE, or 4#E4#E the /0
statement, then EEC3TE the prepared statement.
). Handle any /0 return codes that might result.
The following is an e'ample of an application running a dynamic /0 non-E0ECT
statement stmtstrg&2
EXEC SQL
E+ECUTE IMMEDIATE% stmtstr34
Usi#$ the PREPARE a#( E+ECUTE state,e#ts
5 non-SELEC statements contain no #arameter markers, the6 can be run
76namicall6 usin3 the EXECE 88EDE statement. !o9e0er, i5 the non-
SELEC statements ha0e #arameter markers, the6 must be run usin3 :"E:"E
an7 EXECE.
http://publib.boulder.ibm.com/iseries/v5r1/ic2924/info/sqlp/rbafymst02.htm#ToC_345http://publib.boulder.ibm.com/iseries/v5r1/ic2924/info/sqlp/rbafymst02.htm#ToC_345