Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

FETCH one row at a tiime

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
hcl_ln

New User


Joined: 14 Sep 2005
Posts: 21

PostPosted: Tue Dec 04, 2007 9:55 pm    Post subject: FETCH one row at a tiime
Reply with quote

My SQL query is like this. (declare)

Code:
 
EXEC SQL                                 
 DECLARE CRSR CURSOR WITH HOLD FOR
   SELECT  A.col1,A.col2,A.col3,A.col4
      FROM  table1 as A                                 
    WHERE cond1 and cond2
END-EXEC


Knowing that it returns multiple rows from my FETCH (after open cursor), is there a way just to return one row at a time, do some processing in my cobol pgm; return to fetch for the second row and then do the processing; return to fetch for the thrid row and do the processing; until all the rows from the fetch are done.

I don't know FETCH is that smart to process one record at a time.
Also, I cannot put more conditions in my WHERE clause to strip off the records.

Alternatively, I could do this way.
Unload the table with the above SQL into a flat file and read it in my cobol program. This seems simpler, but the requirement is to do with a cursor in the program.

Thanks for your time,
Back to top
View user's profile Send private message

acevedo

Active User


Joined: 11 May 2005
Posts: 344
Location: Spain

PostPosted: Tue Dec 04, 2007 10:12 pm    Post subject:
Reply with quote

but the requirement is to do with a cursor in the program.
the requirement is a Cursor becouse that's the 'normal' way.

you could use FETCH FIRST ROW ONLY ...but that implies you should modify your WHERE clause.

you talk about unloading..
DECLARE CURSOR-------------------->FD YOURFILE
OPEN CURSOR------------------------->OPEN INPUT YOURFILE
FETCH----------------------------------->READ
CLOSE CURSOR------------------------>CLOSE YOURFILE
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10279
Location: italy

PostPosted: Tue Dec 04, 2007 10:20 pm    Post subject: Reply to: FETCH one row at a tiime
Reply with quote

Quote:
...multiple rows from my FETCH (after open cursor), is there a way just to return one row at a time, d


I believe that there is a little bit of confusion in the terminology..

when the select returns "ONE" row ... OK for the simple select

The "CURSOR technique is/MUST_BE used when a SELECT might return more than one ROW...

The FETCH statement WILL retrieve one row at the time from the rows
selected by the cursor definition ...


... and YES... FETCH is smart because it was made just for that
Back to top
View user's profile Send private message
Phrzby Phil

Active Member


Joined: 31 Oct 2006
Posts: 964
Location: Richmond, Virginia

PostPosted: Tue Dec 04, 2007 11:31 pm    Post subject:
Reply with quote

In other words, simplified:

Look at the cursor as a sequential file and the FETCH as a READ NEXT, with AT END checking done by referencing the SQL code..
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts Multi row fetch - "for read only... Nileshkul DB2 3 Sun Aug 14, 2016 12:52 am
No new posts JCL to fetch schedule status from act... parasmalik20 CA Products 1 Thu Jun 02, 2016 7:11 pm
No new posts ABEND due to FETCH FAILED jackzhang75 ABENDS & Debugging 17 Thu Mar 10, 2016 3:08 am
No new posts Sort Query to fetch particular records RahulG31 DFSORT/ICETOOL 13 Fri Mar 04, 2016 8:17 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us