#include <stdio.h> #include <string.h> EXEC SQL BEGIN DECLARE SECTION; char LOGON_STRING[30]; char USER[30]; char CNAME[30]; long EMPNUM; int SQLCODE; short int EMPIND; VARCHAR errmsg[81]; short USERIND; EXEC SQL END DECLARE SECTION; EXEC SQL INCLUDE sqlca; long errcode; short maxlen = 80; char REQUEST_TYPE[30]; /**************************************************************/ /* DECLARE CURSORS /***************************************************************/ EXEC SQL DECLARE EMPCUR1 CURSOR FOR SELECT EMPLOYEE_NUMBER FROM EMPLOYEE; EXEC SQL DECLARE EMPCUR2 CURSOR FOR SELECT EMPLOYEE_NUMBER FROM EMPLOYEE; EXEC SQL DECLARE EMPCUR3 CURSOR FOR SELECT EMPLOYEE_NUMBER FROM EMPLOYEE; /****************************************************************/ /*ERROR_CHECK - check the sqlcode from the last SQL request made*/ /* and print a message for any non-zero sqlcode */ /****************************************************************/ ERROR_CHECK(char *REQUEST_TYPE) { if (sqlca.sqlcode != 0) { PPRTEXT(&SQL_RDTRTCON, &errcode, &errmsg, &maxlen); errmsg.arr[errmsg.len] = '\0'; printf("\n"); printf("ERROR/WARNING DETECTED IN %s\n", REQUEST_TYPE); printf(" SQL CODE : %d\n", sqlca.sqlcode); printf(" ERROR CODE: %d\n", sqlca.sqlerrd[0]); printf(" MSG : %s\n", errmsg.arr); } } /****************************************************************/ /*LOGON_FUNC - logon to the DBS using the specified LOGON string*/ /* and connection name. */ /****************************************************************/ logon_func(char *string, char *conname) { strcpy(LOGON_STRING, string); strcpy(CNAME, conname); EXEC SQL LOGON :LOGON_STRING AS :CNAME; ERROR_CHECK("LOGON"); if (sqlca.sqlcode != 0) return; } /**************************************************************/ /* SELECT_USER - get the name of this DBS user and store it. */ /**************************************************************/ SELECT_USER() { EXEC SQL SELECT USER INTO :USER INDICATOR :USERIND; ERROR_CHECK("SELECT"); if (sqlca.sqlcode == 0) { USER[29] = '\0'; printf("\n"); printf("USER : %s\n", USER); } } /******************************************************************/ /*SET_CONNECTION - the the programs's current session to the one */ /* associated with the specified connection name */ /******************************************************************/ set_connection(char *conname) { strcpy(CNAME, conname); EXEC SQL SET CONNECTION :CNAME; ERROR_CHECK("SET CONNECTION"); if (sqlca.sqlcode != 0) return; SELECT_USER(); } /**************************************************************/ /* MAIN PROGRAM */ /**************************************************************/ main(int argn, char **log) { /* Create 5 DBS sessions */ logon_func(log[1], "STV2A"); logon_func(log[1], "STV2B"); logon_func(log[1], "STV2C"); logon_func(log[1], "STV2D"); logon_func(log[1], "STV2E"); set_connection("STV2A"); /* Set the current session to STV2A */ /* Initiate the SQL SELECT associated with cursor EMPCUR1, */ /* labeling this asynchronous request SELECT1, but don't wait */ /* for the request to be completed. */ EXEC SQL ASYNC (SELECT1) OPEN EMPCUR1; ERROR_CHECK("OPEN EMPCUR1"); set_connection("STV2B"); /* Set the current session to STV2B */ /* Initiate the SQL SELECT associated with cursor EMPCUR2, */ /* labeling this asynchronous request SELECT2, but don't wait */ /* for the request to be completed. */ EXEC SQL ASYNC (SELECT2) OPEN EMPCUR2; ERROR_CHECK("OPEN EMPCUR2"); set_connection("STV2C"); /* Set the current session to STV2C */ /* Initiate the SQL SELECT associated with cursor EMPCUR3, */ /* labeling this asynchronous request SELECT3, but don't wait */ /* for the request to be completed. */ EXEC SQL ASYNC (SELECT3) OPEN EMPCUR3; ERROR_CHECK("OPEN EMPCUR3"); /* Now, wait here for request SELECT1 to be completed. */ EXEC SQL WAIT SELECT1 COMPLETION; ERROR_CHECK("WAIT SELECT1"); if (sqlca.sqlcode == 0) { printf("\nWAIT FOR SELECT1 REQUEST COMPLETED. \n"); } /* Test for the completion of request SELECT1. */ EXEC SQL TEST SELECT1 COMPLETION; ERROR_CHECK("TEST FOR SELECT1"); set_connection("STV2A"); /* Reset the current session to STV2A */ while (sqlca.sqlcode == 0) /* Retrieve the rows from the SELECT */ { /* associated with cursor EMPCUR1 */ EXEC SQL FETCH EMPCUR1 INTO :EMPNUM INDICATOR :EMPIND; ERROR_CHECK("FETCH EMPCUR1"); if (sqlca.sqlcode == 0) { printf("\nEMPLOYEE NUMBER : %ld\n", EMPNUM); } } /* Now, wait here for all other outstanding asynchronous */ /* requests to be completed (in this case SELECT2 & SELECT3). */ EXEC SQL WAIT ALL COMPLETION; ERROR_CHECK("WAIT ALL"); if (sqlca.sqlcode == 0) { printf("\nWAIT ALL COMPLETED. \n"); } /* Test for the completion of request SELECT2. */ EXEC SQL TEST SELECT2 COMPLETION; ERROR_CHECK("TEST FOR SELECT2"); set_connection("STV2B"); /* Set the current session to STV2B */ while (sqlca.sqlcode == 0) /* Retrieve the rows from the SELECT */ { /* associated with cursor EMPCUR2 */ EXEC SQL FETCH EMPCUR2 INTO :EMPNUM INDICATOR :EMPIND; ERROR_CHECK("FETCH EMPCUR2"); printf("EMPLOYEE NUMBER : %ld\n", EMPNUM); } /* Logoff from the DBS all (5) sessions created by this program */ EXEC SQL LOGOFF ALL; ERROR_CHECK("LOGOFF"); }