/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <stdlib.h>

EXEC SQL INCLUDE SQLCA;

#define CHECK(sql) if(sqlca.sqlcode != 0) \
                   { \
                       printf("Error in " sql "\n"); \
                       exit(EXIT_FAILURE); \
                   }

int main()
{
    EXEC SQL BEGIN DECLARE SECTION;
    char f2[50];
    long int n;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL CONNECT TO test;
    CHECK("CONNECT TO")
    EXEC SQL DECLARE c CURSOR FOR SELECT f2,COUNT(*) AS n FROM t1 WHERE f1 > 1 GROUP BY f2;
    CHECK("DECLARE CURSOR FOR SELECT")
    EXEC SQL OPEN c;
    CHECK("OPEN CURSOR")
    for(;;)
    {
        EXEC SQL FETCH c INTO :f2, :n;
        if(sqlca.sqlcode != 0) break;
        printf("%s %d\n", f2, n);
    }
    EXEC SQL CLOSE c;
    return EXIT_SUCCESS;
}
