You are on page 1of 4

9/25/2018 Multiple row fetch using cursors in DB2 | Mainframes Simplified

Mainframes Simplified search

Sidebar Home About the Author

TSO SAVE Com… 1


Multiple row fetch using
Mainframe for Du… 2
cursors in DB2
A cursor is used to fetch single row a
Multiple row fetch using … time from the database. By using a
cursor you loose take capability of
performance tuning your application.
Executing only a … 2
You can optimize your application by
using a row-set positioned cursor. A
How to rename al… 1
row-set positioned cursor can retrieve
mutiple rows of data during fetch.

Row-set positioned cursor


The advantages of using a row-set
positioned cursor are
It reduces the number of
statements issued between your
program's adress space and DB2
It reduces the number od
statements issued between DB2
and the DDF address space
It improves throughput, lowers
network operations and CPU
consumption
The row-set positioned cursor can be
incorporated in your DB2-COBOL
program with much ease.It is very
similar to the normal cursor. In a
normal cursor we store the data
fetched in to the host varaible but
incase of the row-set positioned cursor
we store the data fetched into a host
variable array.

Lets take an example of a simple


employee table DB2.EMPLOYEE which
has the capability to store only 100
rows in it.

EMP_NO [char (6)] EMP_NAME


[char (20)]
E10001 John

http://mainframes-simplified.blogspot.com/2011/07/multiple-row-fetch-using-cursors-in-db2.html 1/4
9/25/2018 Multiple row fetch using cursors in DB2 | Mainframes Simplified

Felton
E10002 Greg
Hamilton
E10003 Denise
Hall
.... ....
.... ....
.... ....
E10100 Jack
Nicolson
[] You will have to add the following to
your COBOL program

Working Storage Section


*Declare the host array to store the
employee details in a single fetch
01 HOST-ARRAY.
10 EMP-NO PIC
X(6) OCCURS 100 TIMES.
10 EMP-NAME
PIC X(20)OCCURS 100 TIMES.

*Declare the subcript varaible to


access the array elements
01 SUB-I PIC S9(4) COMP VALUE
ZERO.

*Declare the cursor


EXEC SQL
DECLARE EMP-DATA CURSOR
WITH ROWSET POSITIONING FOR
SELECT EMP_NO, EMP_NAME
FROM DB2.EMPLOYEE
END-EXEC

Procedure Division
*Open the cursor
OPEN EMP-DATA CURSOR

*Fetch the data in a single go


EXEC SQL
FETCH NEXT ROWSET
FROM EMP_DATA FOR 100 ROWS
INTO :EMP-NO, :EMP-NAME
END-EXEC.

*Use the data fetched


PERFORM 100 TIMES
ADD +1 TO SUB-I
http://mainframes-simplified.blogspot.com/2011/07/multiple-row-fetch-using-cursors-in-db2.html 2/4
9/25/2018 Multiple row fetch using cursors in DB2 | Mainframes Simplified

DISPLAY EMP-NO(SUB-I)
DISPLAY EMP-NAME(SUB-I)
END-PERFORM

Thats it. We got all the rows in a single


fetch.Isn't it simple?

What if...
If there are only 96 rows instead of
100 then SQLERRD(3) can be used to
access only the 96 elements in the
array and the remaining 4 elements
can be skipped.Remember that
SQLERRD(3) contains the number of
rows retrieved.

If there are 10000 records in a table


then fetch 1000 rows (FETCH NEXT
ROWSET FROM EMP_DATA FOR 1000
ROWS)at a time.Wisely decide how
many rows to be fetched at a time
depending on the data volume in the
table.

Sumit says...

[http://2.bp.blogspot.com/-
c5cJ335IWz8/TjKLTtaSZUI/AAAAAAAABx8/rtUd6
_mDGOE/s1600/sumit.bmp]

Posted 29th July 2011 by em jay


Labels: adress space, array, close, CPU,
cursor, database, DB2, DB2-COBOL, DDF,
declare, fetch, FETCH NEXT, mainframes,
open, Procedure Division, Row-set
postioned cursor, rows, SQLERRD(3),
Working Storage

http://mainframes-simplified.blogspot.com/2011/07/multiple-row-fetch-using-cursors-in-db2.html 3/4
9/25/2018 Multiple row fetch using cursors in DB2 | Mainframes Simplified

0 Add a comment

Enter your comment...

Comment as:
RanmaHalf (Google)

Sign out

Publish Notify me

Preview

http://mainframes-simplified.blogspot.com/2011/07/multiple-row-fetch-using-cursors-in-db2.html 4/4

You might also like