#include<stdio.h>
#include<string.h>
EXEC SQL BEGIN DECLARE SECTION;
char SQLSTATE[6];
char SQLMSG[256];
VARCHAR *country;
short loop;
EXEC SQL END DECLARE SECTION;
#define MAX_COUNTRY 100
void putmsgx(void);
main(void){
EXEC SQL WHENEVER SQLERROR GOTO :CONNECT_ERROR;
EXEC SQL CONNECT TO 'DB01';
EXEC SQL WHENEVER SQLERROR GOTO :STM_ERROR;
EXEC SQL WHENEVER NOT FOUND GOTO :NOT_FOUND_ERROR;
country = (struct country_SQLVAR *)malloc(256);
printf("すべての国名を取り出します.\n");
for ( loop=0; loop<MAX_COUNTRY; loop++ ) {
country->sqllen = 254;
EXEC SQL SELECT COUNTRY INTO :country
FROM SCH.TBL
WHERE DATANO = :loop;
country->sqlvar[country->sqllen] = '\0';
printf("%d件目の国名は%sです.\n", loop, country->sqlvar );
}
NOT_FOUND_ERROR:
STM_ERROR:
putmsgx();
EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL COMMIT WORK;
EXEC SQL DISCONNECT 'DB01';
return 1;
CONNECT_ERROR:
putmsgx();
return 1;
}
void putmsgx( void ) {
int i;
for ( i = sizeof(SQLMSG) - 2 ; SQLMSG[i] == ' ' ; i-- );
SQLMSG[++i] = '\0';
SQLSTATE[5]='\0';
printf( "SQLMSG:%s\n" , SQLMSG );
printf( "SQLSTATE:%s\n" , SQLSTATE );
return;
} |