fetch cursor and cursor with return.

RDBMS from IBM and IBM's Hierarchical DBMS and Transaction Manager (IMS DC).
Post Reply
Sankar Sabari
Registered Member
Posts: 22
Joined: Thu Mar 17, 2016 9:54 pm

fetch cursor and cursor with return.

Post by Sankar Sabari »

Hi All,

What is difference between fetch cursor and cursor with return for please help on this.
Sankar Sabari
Registered Member
Posts: 22
Joined: Thu Mar 17, 2016 9:54 pm

Re: fetch cursor and cursor with return.

Post by Sankar Sabari »

Hi,

Has someone used this before, if yes please help.
User avatar
zum13
Registered Member
Posts: 85
Joined: Thu May 04, 2023 12:58 am

Re: fetch cursor and cursor with return.

Post by zum13 »

The most common way of using a cursor consists of a DECLARE CURSOR for a SELECT statement that will read a number of rows, an OPEN to start processing, a number of iterations of FETCH to retrieve the individual rows, then a CLOSE to terminate processing on the cursor. This will all be done within the same program.

The WITH RETURN option on a cursor declaration is used from a stored procedure and allows a particular result set to be returned to the caller. The stored procedure will declare and open a cursor then return it as a result set to the calling program. The caller uses ALLOCATE to translate the result set reference into a cursor at which point the data can be processed in the same way as before.
User avatar
Anuj Dhawan
Founder
Posts: 2802
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: fetch cursor and cursor with return.

Post by Anuj Dhawan »

In addition to what zum13 said, this from the manuals can be of interest:
WITHOUT RETURN or WITH RETURN
Specifies whether the result table of the cursor is intended to be used as a result set that will be returned from a procedure. If statement-name is specified, the default is the corresponding prepare attribute of the statement. Otherwise, the default is WITHOUT RETURN.

WITHOUT RETURN
Specifies that the result table of the cursor is not intended to be used as a result set that will be returned from a procedure.

WITH RETURN
Specifies that the result table of the cursor is intended to be used as a result set that will be returned from a procedure. WITH RETURN is relevant only if the DECLARE CURSOR statement is contained within the source code for a procedure. In other cases, the precompiler might accept the clause, but it has not effect.

When a cursor that is declared using the WITH RETURN TO CALLER clause remains open at the end of a program or routine, that cursor defines a result set from the program or routine. Use the CLOSE statement to close a cursor that is not intended to be a result set from the program or routine. Although Db2 will automatically close any cursors that are not declared using with a WITH RETURN clause, the use of the CLOSE statement is recommended to increase the portability of applications.

For non-scrollable cursors, the result set consists of all rows from the current cursor position to the end of the result table. For scrollable cursors, the result set consists of all rows of the result table.
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Sankar Sabari
Registered Member
Posts: 22
Joined: Thu Mar 17, 2016 9:54 pm

Re: fetch cursor and cursor with return.

Post by Sankar Sabari »

Thankyou both. This is helpful.
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM DB2 and IMS DB/DC”