Вы находитесь на странице: 1из 3

9/24/2018 IBM MAINFRAME FORUM: CURSORS in DB2 and how to use CURSOR in DB2?

More vivekmishra8086@gmail.com Dashboard Sign Out

This blog intends to write on IBM Mainframe Technologies and cover key concepts in Cobol,DB2,CICS,JCL,VSAM and more..

Home Tips&Tricks Useful Links My contact

Tuesday, May 6, 2014

Type Here... Search


CURSORS in DB2 and how to use CURSOR in DB2?
Get posts in your email
A mechanism allowing an application program (cobol-db2 program for example) to retrieve a set of
rows. Suppose our SQL query fetches many-rows result table. Using CURSOR our COBOL program Email address... Submit
can gain access to individual row of the result table.

Blog Posts
There are two types of cursor:
1.Row-set positioned cursor : Here the result table are stored into Host variable arrays. DB2 (34) JCL (25)
2.Row-positioned cursor : This type points to the current row/particular row in the result set. COBOL (15) SORT (11) ISPF
It points to the next row to be passed to the application program. (10) SAS (9) VSAM (8) CA7 (3) CICS
(3) REXX (1) SQL (1)
The primary steps for using any cursor are as follows:
Step1. Declare the cursor Followers
Step2 Open the Cursor. This causes Db2 to build the result set.
Followers (35) Next
Step3. Fetch each row ,one at a time, and pass it onto application program
Step4: Close the cursor.

DECLARE the cursor:


EXEC SQL
DECLARE EMP_CURSOR CURSOR FOR
SELECT EMP_NAME,EMP_ID
FROM EMP
WHERE EMP_JOIN_DATE > '20000101' Follow
END-EXEC.
DECLARE statement does not build result set.It simply forms the SQL statement we want to use to
My unique Blog visits
fetch the data. At DB2 precompile time, the definition of the CURSOR is stored in the bindfile, and the
statement is commented.
322,742
OPEN the cursor:
The open statement actually runs the SQL statement and builds the result set and makes it ready Blog Archive

ready to be processed to be used to the application program in subsequent steps. ► 2013 (25)
EXEC SQL
▼ 2014 (34)
OPEN EMP_CURSOR
► February (4)
END-EXEC.
► March (8)
Once the open is done, we need to fetch the data
► April (3)
FETCH the cursor: ▼ May (4)
EXEC SQL SAS in Mainframes(z/Os)
FETCH EMP_CURSOR Tutorial : Use of symbolic...
INTO :EMPNAME,:EMPID CURSORS in DB2 and how to
END-EXEC. use CURSOR in DB2?
Scrollable Cursor in DB2 :
Sensitive cursor,insens...
FETCH is the only way to get the result into application program Host variable. We fetch each row into
the Host variable and process the data. We can continue fetching of the data until we get a SQLCODE Static cursors and Dynamic
cursors in DB2
of +100 which implies END of the result table.
CLOSE the CURSOR ► June (4)
Once we are done with fetching of the records, we need to close the cursor.
► July (1)
EXEC SQL
► August (4)
CLOSE EMP_CURSOR
END-EXEC. ► September (1)
► October (2)

http://mframes.blogspot.com/2014/05/normal-0-false-false-false.html 1/3
9/24/2018 IBM MAINFRAME FORUM: CURSORS in DB2 and how to use CURSOR in DB2?
How to UPDATE AND DELETE records in cursor. ► December (3)
There can be instances where we need to modify the data in db2 table based on some values in a
programmatic way. Had it been a spufi or QMF we have our update and delete statements ready to run ► 2015 (23)
and modify. But we are in cobol program. Nevertheless we can do the same modifications using ► 2016 (9)
cursors again.!! We need to add special clause while declaring the cursor with 'FOR UPDATE OF'. ► 2017 (1)
Even if we want to delete, we need to mention 'FOR UPDATE OF' in declare statement and we
need to delete the entire row. Also we need to use WHERE CURRENT OF to update or delete the last
row fetched.
Lets see the below example where we will update and delete some row in the cursor :

EXEC SQL
DECLARE EMP_CURSOR CURSOR FOR
SELECT EMP_NAME,EMP_ID
FROM EMP
WHERE EMP_JOIN_DATE > '20000101'
FOR UPDATE OF NAME <== DECLARE THE CURSOR WITH THIS CLAUSE
END-EXEC.
...............................
EXEC SQL
UPDATE EMP
SET EMP_NAME=:EMPNAME
WHERE CURRENT OF EMP_CURSOR
END-EXEC

EXEC SQL
DELETE FROM EMP
WHERE CURRENT OF EMP_CURSOR
END-EXEC.
****Important point Regarding the Usage of 'FOR UPDATE OF' in CURSOR.****
If the select statement defining the cursor contains ORDER BY,GROUP BY, DISTINCT, SET
OPERATORS ( UNION,EXCEPT, INTERSECT), JOIN, FOR FETCH ONLY/FOR READ ONLY, then the
usage of 'FOR UPDATE OF' is NOT permitted. The above keywords make the cursor READ ONLY
cursor.

Read about Cursor types: Sensitive and insensitive cursors !

Posted by Rikdeb Mukherjee

Labels: COBOL, DB2

2 comments:

Dhanaji Patil 12/28/2016 01:31:00 AM


Nice information... With example..Thank you!
Reply

Saurabh Srivastava 4/07/2017 11:07:00 AM


I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual)
instructor lead live training in TECHNOLOGY , kindly contact us http://www.maxmunus.com/contact
MaxMunus Offer World Class Virtual Instructor led training on TECHNOLOGY. We have industry expert
trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted
100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain
and UAE etc.
For Demo Contact us.
Saurabh Srivastava
MaxMunus
E-mail: saurabh@maxmunus.com
Skype id: saurabhmaxmunus
Ph:+91 8553576305 / 080 - 41103383
http://www.maxmunus.com/

Reply

http://mframes.blogspot.com/2014/05/normal-0-false-false-false.html 2/3
9/24/2018 IBM MAINFRAME FORUM: CURSORS in DB2 and how to use CURSOR in DB2?

Enter your comment...

Comment as: RanmaHalf (Google) Sign out

Publish Preview Notify me

Links to this post


Create a Link

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

Interpreting TSO ISRDDN command in mainframe ISPF


Zoned Decimal and packed Decimal (COMP- 3) fields. How Does COMP 3 saves storage space?
COALESCE function in DB2 . Alternative to NULL indicator variable in DB2 ; SQLCODE - 305.
RENT compiler option in cobol - Understanding reentrant programming
FETCH FIRST N ROWS Vs OPTIMIZE FOR N ROWS in DB2

Powered by Blogger.

http://mframes.blogspot.com/2014/05/normal-0-false-false-false.html 3/3

Вам также может понравиться