简体   繁体   中英

Why is my cursor infinitely looping on the last row

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM