IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Can the FEtch statement used like below


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
pravin madoori

New User


Joined: 29 Jan 2007
Posts: 25
Location: Hyderabad

PostPosted: Tue Sep 23, 2008 6:12 pm
Reply with quote

Let I have a table with 5 columns & cursor declaration like below.Does the below FETCH to use a curosor correct?

EXEC SQL DECLARE CSR1 CURSOR FOR
SELECT Col1,col2,col3,col4,col5 FROM table1;

EXEC SQL OPEN CSR1;
EXEC SQL FETCH CSR1 INTO :COL2,:COL3; /*IS IT correct, using only two colums here, where in declaration there were all columns */
DO WHILE (SQLCODE = 0);

EXEC SQL FETCH CSR1 INTO :COL2,:COL3;

END;
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Tue Sep 23, 2008 6:45 pm
Reply with quote

Yes you can ....
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Tue Sep 23, 2008 7:03 pm
Reply with quote

SQL Reference wrote:
The first value in the result row is assigned to the first host variable, the second value to the second host variable, and so on.

Even if this works (which I haven't personally checked yet),
your COL2 variable will receive value of field "col1", and COL3 variable will receive "col2" value.
Maybe that's not exactly what you want ...
Back to top
View user's profile Send private message
pravin madoori

New User


Joined: 29 Jan 2007
Posts: 25
Location: Hyderabad

PostPosted: Tue Sep 23, 2008 7:16 pm
Reply with quote

Yes, I have checked it, the fetch can be used like above but results would be wrong.

If the fetch was like below, it returns correct values to colums 1 & 2.

EXEC SQL FETCH CSR1 INTO :COL1,:COL2;
Back to top
View user's profile Send private message
saquib.ahsan

New User


Joined: 22 Sep 2008
Posts: 6
Location: Pune

PostPosted: Tue Sep 23, 2008 9:13 pm
Reply with quote

I checked this as well and it works fine. However, the value that col2 will receive is the value present in the col1 field of the table. same thing applies to the other column. I am not sure if that is what you intend for..
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Sep 24, 2008 1:08 am
Reply with quote

Hello saquib.ahsan and welcome to the forum,

Quote:
I checked this as well and it works fine. However, the value that col2 will receive is the value present in the col1 field
It might execute, but i would not call it "fine".

At best it will cause an abend that needs immediate attention. At worst, it may cause data corruption. . .
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Fetch data from programs execute (dat... DB2 3
No new posts Code Multi Row fetch in PL1 program PL/I & Assembler 1
No new posts Need to fetch data from so many DB2 t... DB2 9
No new posts JOIN STATEMENT PERFORMANCE. DFSORT/ICETOOL 12
No new posts Relate COBOL statements to EGL statement All Other Mainframe Topics 0
Search our Forums:

Back to Top