How can I fetch all results from a query with a cursor?

286 Views Asked by At

I'm doing some homework where I'm using SQL with ProC/C++ and I'm using a template given by my professor from a previous assignment. The problem is that the function that displays the results of a query only seems to output a single tuple. I want it to output all of the fetched data, but the syntax is a bit foreign to me.

My query:

strcat((char *)sqlQueryToRun," select employeeName from Employees");

The execute function:

void  prepareAndExecuteIt() {

//  Prepare the query

    //sqlQueryToRun.len = strlen((char *) sqlQueryToRun.arr);
    exec sql PREPARE dbVariableToHoldQuery FROM :sqlQueryToRun;

/* The declare statement, below, associates a cursor with a
 * PREPAREd statement.  
 * The cursor name, like the statement
 * name, does not appear in the Declare Section.
 * A single cursor name can not be declared more than once.
*/

    exec sql declare cursorToHoldResultTuples cursor FOR dbVariableToHoldQuery;

    exec sql open cursorToHoldResultTuples  ;

    exec sql FETCH cursorToHoldResultTuples INTO :outputSupplierName;

    printf("\nSupplier name: %s\n", outputSupplierName);
    exec sql close cursorToHoldResultTuples;
}

This only outputs a single name when I know there are more in this table. From what I've read there should be some kind of fetchall() method that I can use, but I'm not sure how to apply it with this syntax.

Trying to loop the fetch statement looks like this and gives the following error message:

for (int i = 0; i < 10; i++){
        exec sql FETCH cursorToHoldResultTuples INTO :outputSupplierName;
        printf("%s\n", outputSupplierName);
    }

make[1]: *** [/home/cs/prof/4620/myMake1.mk:104: pc1] Error 1

make: *** [/home/cs/prof/4620/myMake1.mk:110: HW6.o] Error 2

0

There are 0 best solutions below