tags:

views:

103

answers:

1

Hello I've been trying to use a cursor with embedded sql in c but I can't seem to get it to stop reading the last row in my table. The table is called publication with two attributes pubid and title. I just want my cursor to iterate through and display the pubid. This is what i have:

    EXEC SQL DECLARE C1 CURSOR FOR SELECT pubid FROM publication;
    EXEC SQL OPEN C1;
    while(SQLCODE !=100){
        EXEC SQL FETCH C1 INTO :pubid; //a host variable that was declared earlier
        cout<<pubid<<endl;
    }

When I run, it displays all the rows and infinitely repeats displaying the last row. I tried displaying the SQLCODE as well and it remains 0, so I'm not sure why the cursor doesn't move past the last row

+1  A: 
EXEC SQL DECLARE C1 CURSOR FOR SELECT pubid FROM publication;
EXEC SQL OPEN C1;
EXEC SQL WHENEVER NOT FOUND GOTO close_c1;
while(SQLCODE !=100) {
    EXEC SQL FETCH C1 INTO :pubid;
    cout<<pubid<<endl;
}
close_c1:
EXEC SQL CLOSE C1;

Something like this should work. Also consider using EXEC SQL WHENEVER SQLERROR clean_up_function; to be able to print out diagnostics. I found references here.

vpit3833
Your way was actually my first attempt, but for some reason the compiler would give me this error: label 'close_c1' used but not defined so I decided to go this way
Dave
Can't come up with any reason why that error should occur. Is the label defined in the same function as it is called from? Defining the label outside the block of a for loop or while loop is OK.
vpit3833
Yea the label is in the same function
Dave