#include<stdio.h>
#include<string.h>
EXEC SQL BEGIN DECLARE SECTION;
char SQLSTATE[6];
char SQLMSG[256];
VARCHAR strsql[100];
VARCHAR cmd[100];
VARCHAR cur[100];
VARCHAR country[100];
short loop;
EXEC SQL END DECLARE SECTION;
#define MAX_COUNTRY 100
#define SQL_NUM 10
void putmsgx(void);
main( void ){
char data[SQL_NUM][100] = {
"SELECT COUNTRY FROM SCH.TBL00",
"SELECT COUNTRY FROM SCH.TBL01",
"SELECT COUNTRY FROM SCH.TBL02",
"SELECT COUNTRY FROM SCH.TBL03",
"SELECT COUNTRY FROM SCH.TBL04",
"SELECT COUNTRY FROM SCH.TBL05",
"SELECT COUNTRY FROM SCH.TBL06",
"SELECT COUNTRY FROM SCH.TBL07",
"SELECT COUNTRY FROM SCH.TBL08",
"SELECT COUNTRY FROM SCH.TBL09"};
char work_area[10];
char indata[10];
EXEC SQL WHENEVER SQLERROR GOTO :CONNECT_ERROR;
EXEC SQL CONNECT TO 'DB01';
EXEC SQL WHENEVER SQLERROR GOTO :STM_ERROR;
for ( loop=0; loop<SQL_NUM; loop++ ) {
strcpy( (char *)strsql.sqlvar, (char *)data[loop] );
strsql.sqllen = strlen(strsql.sqlvar);
sprintf( cmd.sqlvar, "CMD%d", loop );
cmd.sqllen = strlen(cmd.sqlvar);
sprintf( cur.sqlvar, "CUR%d", loop );
cur.sqllen = strlen(cur.sqlvar);
EXEC SQL PREPARE :cmd FROM :strsql;
EXEC SQL ALLOCATE :cur CURSOR FOR :cmd;
}
EXEC SQL WHENEVER NOT FOUND GOTO :NODATA;
for(;;){
printf( "何番目の表のデータを表示するか入力してください (0-9)>> " );
fgets( indata, 10, stdin );
printf("\n");
if( ( indata[0] < '0' ) || ( '9' < indata[0] ) ){
printf( "入力されたデータに誤りがあります\n" );
continue;
}
sprintf( cur.sqlvar, "CUR%c", indata[0] );
cur.sqllen = strlen(cur.sqlvar);
EXEC SQL OPEN :cur;
for(loop=1;;loop++){
EXEC SQL FETCH :cur INTO :country;
country.sqlvar[country.sqllen] = '\0';
printf("%d件目の国名は%sです.\n", loop, country.sqlvar );
}
NODATA:
EXEC SQL CLOSE :cur;
EXEC SQL COMMIT WORK;
printf("終了しますか?(Y/y) >>");
fgets( indata, 10, stdin );
printf("\n");
if(indata[0]=='Y' || indata[0]=='y'){
break;
}
}
EXEC SQL COMMIT WORK;
EXEC SQL DISCONNECT 'DB01';
return 0;
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;
} |