Re: FETCH all the records in the table

From: Gerard H. Pille <ghp_at_infosoft.be>
Date: 1996/03/26
Message-ID: <4j9epk$if1_at_news.Belgium.EU.net>#1/1


In article <4j76c2$1je0_at_news.missouri.edu>, Ravi Kodali (kodali_at_piglet.coe.missouri.edu) says...
!>
!>Hi,
!>
!>I need some help with the problem of fetching records in the
!>tables in Pro*C.
!>
!>I can declare a cursor like this...
!>
!>main() {
!>
!>getentry();
!>}
!>
!>getentry() {
!>
!> EXEC SQL CONNECT :DB;
!> EXEC SQL DECLARE users CURSOR FOR
!> SELECT * FROM USER_INFO;
!>
!> EXEC SQL OPEN users;
!>
!> while(1) {
!>
!> EXEC SQL WHENEVER NOT FOUND DO break;
!> EXEC SQL FETCH users INTO :structurename;
!> }
!>}
!>
!> but if I want to return to main() after fetching the
!> first record and then comeback and fetch the second
!> record and so on... how do I do that. IF users is a
!> c structure, I would have had a pointer but users is
!> a SQL identifier and so how do I do that ? Once I quit
!> the getentry() function after fetching the first record
!> the function memory is gone and it is fresh for the next
!> call.
!>
!> Please let me know even if there is another way of doing
!> it.
!>
!> please reply to kodali_at_coe.missouri.edu and if u reply to
!> the posting it would bounce due to various reasons.
!>
!> thank you and please help me.
!>
!> Ravi

I can't resist it when you ask it so politely:

EXEC SQL WHENEVER NOT FOUND DO break;
EXEC SQL DECLARE users CURSOR FOR

    SELECT * FROM USER_INFO; getentry() {  

 while(1) {

   EXEC SQL FETCH users INTO :structurename;

   /* treat each fetched record */
   }
}

main() {

 EXEC SQL CONNECT :DB;
 EXEC SQL OPEN users;

getentry(); /* will now treat all user records */ }

You should take into account that the EXEC SQL's are pre-compiler directives, not c instructions. An EXEC SQL WHENEVER counts until you find another in your source, the scope is totally different from what you're used to in c. Also you reopened the cursor

If you want 'getentry' to treat only one record, remove the while.

-- 
Kind reGards
     \ /   |
      X    |
     / \   s
     Gerard
Received on Tue Mar 26 1996 - 00:00:00 CET

Original text of this message