Example 1: Using a parameter for a statement name . . . * This program prepares a statement for dynamic execution from the string * passed to it, and uses a dynamic cursor to fetch a row from a table. * */ #include <stdio.h> #include <descrip.h> struct SQLDA_STRUCT { char SQLDAID[8]; int SQLDABC; short SQLN; short SQLD; struct { short SQLTYPE; short SQLLEN; char *SQLDATA; short *SQLIND; short SQLNAME_LEN; char SQLNAME[30]; } SQLVAR[]; } *SQLDA; main() { /* * General purpose locals */ int i; long sqlcode; char command_string[256]; /* * Allocate SQLDA structures. */ SQLDA = malloc(500); SQLDA->SQLN = 20; /* Get the SELECT statement at run time. */ printf("\n Enter a SELECT statement.\n"); printf("\n Do not end the statement with a semicolon.\n"); gets(command_string); /* Prepare the SELECT statement. */ PREP_STMT( &sqlcode, &command_string, SQLDA ); if (sqlcode != 0) goto err; /* Open the cursor. */ OPEN_CURSOR( &sqlcode ); if (sqlcode != 0) goto err; /* Allocate memory. */ for (i=0; i < SQLDA->SQLD; i++) { SQLDA->SQLVAR[i].SQLDATA = malloc( SQLDA->SQLVAR[i].SQLLEN ); SQLDA->SQLVAR[i].SQLIND = malloc( 2 ); } /* Fetch a row. */ FETCH_CURSOR( &sqlcode, SQLDA ); if (sqlcode != 0) goto err; /* Use the SQLDA to determine the data type of each column in the row and print the column. For simplicity, test for only two data types. CHAR and INT. */ for (i=0; i < SQLDA->SQLD; i++) { switch (SQLDA->SQLVAR[i].SQLTYPE) { case SQLDA_CHAR; /* Character */ printf( "%s", SQLDA->SQLVAR[i].SQLDATA ); break; case SQLDA_INTEGER: /* Integer */ printf( "%d", SQLDA->SQLVAR[i].SQLDATA ); break; default: printf( "Some other datatype encountered\n"); } } /* Close the cursor. */ CLOSE_CURSOR( &sqlcode ); ROLLBACK(&sqlcode ); return; . . . } Example 2: SQL module file that the preceding program calls -- This program uses dynamic cursors to fetch a row. -- -- MODULE C_MOD_DYN_CURS LANGUAGE C AUTHORIZATION RDB$DBHANDLE DECLARE ALIAS FOR FILENAME personnel -- Declare the dynamic cursor. Use a statement name to identify a -- prepared SELECT statement. DECLARE CURSOR1 CURSOR FOR STMT_NAME -- Prepare the statement from a statement entered at run time -- and specify that SQL write information about the number and -- data type of select list items to the SQLDA. PROCEDURE PREP_STMT SQLCODE COMMAND_STRING CHAR (256) SQLDA; PREPARE STMT_NAME SELECT LIST INTO SQLDA FROM COMMAND_STRING; PROCEDURE OPEN_CURSOR SQLCODE; OPEN CURSOR1; PROCEDURE FETCH_CURSOR SQLCODE SQLDA; FETCH CURSOR1 USING DESCRIPTOR SQLDA; PROCEDURE CLOSE_CURSOR SQLCODE; CLOSE CURSOR1; PROCEDURE ROLLBACK SQLCODE; ROLLBACK;