/******************************************************* ** ** A sample program that demonstrates the use of Dynamic embedded SQL. ** Before compiling this program, be sure you have created a table ** called video and inserted some tuples in it. ** ********************************************************/ #include EXEC SQL INCLUDE SQLCA; /* The next statement is used in dynamic SQL programs */ /* where the output (the number of columns or their */ /* types) is not fixed in the compile time. Here we */ /* don't need it, since the output is fixed. */ /* */ /* EXEC SQL INCLUDE SQLDA; */ EXEC SQL BEGIN DECLARE SECTION; /* declare host variables */ char db_name[8]; char qstring[100]; /* buffer for SQL query */ char video_title[30]; short video_id; char director[20]; EXEC SQL END DECLARE SECTION; EXEC SQL WHENEVER NOT FOUND GOTO label; void main() { strcpy(db_name, "c343d30"); EXEC SQL CONNECT TO :db_name; if (sqlca.sqlcode != 0) { printf("Connect failed!: reason %ld\n", sqlca.sqlcode); exit(1); } /* construct a query */ strcpy(qstring, "SELECT video_title FROM c343d30.video"); /* Prepare the query in the run time */ EXEC SQL PREPARE Q1 FROM :qstring; if (SQLCODE != 0) { printf("PREPARE failed!: reason %s\n", sqlca.sqlstate); exit(1); } EXEC SQL DECLARE c1 CURSOR FOR Q1; EXEC SQL OPEN c1; do { EXEC SQL FETCH c1 into :video_title; printf("%s\n",video_title); } while (1); label: EXEC SQL CLOSE c1; EXEC SQL CONNECT RESET; }