/******************************************************************************* ** ** Source File Name = fillcli.sqc 1.6 ** ** Licensed Materials - Property of IBM ** ** (C) COPYRIGHT International Business Machines Corp. 1995, 1999 ** All Rights Reserved. ** ** US Government Users Restricted Rights - Use, duplication or ** disclosure restricted by GSA ADP Schedule Contract with IBM Corp. ** ** ** PURPOSE : ** An stored procedure example which demonstrates the usage of SQLDA ** by passing information to the server procedure on which table to ** fill with random data. (server program : "fillsrv") ** ** EXTERNAL DEPENDENCIES : ** - Ensure existence of database for precompile purposes. ** - Precompile with the SQL precompiler (PREP in DB2) ** - Bind to a database (BIND in DB2) ** - Compile and link with the IBM Cset++ compiler (AIX and OS/2) ** or the Microsoft Visual C++ compiler (Windows) ** or the compiler supported on your platform. ** ** For more information about these samples see the README file. ** ** For more information on programming in C, see the: ** - "Programming in C and C++" section of the Application Development Guide ** For more information on Building C Applications, see the: ** - "Building C Applications" section of the Application Building Guide. ** ** For more information on the SQL language see the SQL Reference. ** *******************************************************************************/ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> #include <sql.h> #include <sqlca.h> #include <sqlda.h> #include <sqlenv.h> #include "util.h" #define CHECKERR( CE_STRING ) if ( check_error( CE_STRING, &sqlca ) != 0 ) return -1 ; int main( int argc, char * argv[] ) { EXEC SQL INCLUDE SQLCA ; EXEC SQL BEGIN DECLARE SECTION ; char database[9] ; char userid[9] ; char passwd[19] ; char procname[33] ; char sql_buffer[255] ; EXEC SQL END DECLARE SECTION ; struct sqlda * client_sqlda, * server_sqlda ; short org_columns, org_rows, i ; if ( argc != 4 ) { printf( "\nUSAGE: fillcli database userid passwd\n\n" ) ; return( 1 ) ; } strcpy( database, argv[1] ) ; strcpy( userid, argv[2] ) ; strcpy( passwd, argv[3] ) ; EXEC SQL CONNECT TO :database USER :userid USING :passwd ; CHECKERR( "CONNECT TO SAMPLE" ) ; printf( "\n" ) ; init_da( &client_sqlda, 1 ) ; init_da( &server_sqlda, 3 ) ; server_sqlda->sqld = 3 ; server_sqlda->sqlvar[0].sqltype = SQL_TYP_NCSTR ; server_sqlda->sqlvar[0].sqllen = 15 ; server_sqlda->sqlvar[1].sqltype = SQL_TYP_NSMALL ; server_sqlda->sqlvar[1].sqllen = sizeof( short ) ; server_sqlda->sqlvar[2].sqltype = SQL_TYP_NCSTR ; server_sqlda->sqlvar[2].sqllen = 13 ; alloc_host_vars( server_sqlda ) ; strcpy( ( char * ) server_sqlda->sqlvar[0].sqldata, "INSERT" ) ; org_rows = 10 ; memcpy( ( void * ) server_sqlda->sqlvar[1].sqldata, ( void * ) &org_rows, sizeof( short ) ) ; strcpy( ( char * ) server_sqlda->sqlvar[2].sqldata, "Tasmania" ) ; memset( ( void * ) sql_buffer, 0, 255 ) ; strcpy( sql_buffer, "SELECT * FROM ORG" ) ; EXEC SQL PREPARE st FROM :sql_buffer ; CHECKERR( "PREPARE SELECT FROM ORG" ) ; EXEC SQL DESCRIBE st INTO :*client_sqlda ; org_columns = client_sqlda->sqld ; free( client_sqlda ) ; if ( org_columns > 0 ) init_da( &client_sqlda, org_columns ) ; EXEC SQL DESCRIBE st INTO :*client_sqlda ; CHECKERR( "DESCRIBE DESCRIPTOR" ) ; alloc_host_vars( client_sqlda ) ; EXEC SQL DECLARE c1 CURSOR FOR st ; EXEC SQL OPEN c1 ; CHECKERR( "OPEN CURSOR" ) ; EXEC SQL FETCH c1 USING DESCRIPTOR :*client_sqlda ; CHECKERR( "OPEN CURSOR" ) ; display_col_titles( client_sqlda ) ; i = 0 ; while ( SQLCODE == 0 ) { display_da( client_sqlda ) ; EXEC SQL FETCH c1 USING DESCRIPTOR :*client_sqlda ; i++ ; } EXEC SQL CLOSE c1 ; CHECKERR( "CLOSE CURSOR" ) ; printf( "\n %d record(s) selected.\n\n", i ) ; memset( ( void * ) procname, 0, 33 ) ; strcpy( procname, "fillsrv" ) ; EXEC SQL CALL :procname USING DESCRIPTOR :*server_sqlda ; CHECKERR( "CALL INSERT WITH SQLDA" ) ; printf( "Server Procedure Complete.\n" ) ; printf( " %d record(s) added.\n\n", org_rows ) ; EXEC SQL COMMIT ; CHECKERR( "COMMIT INSERT" ) ; EXEC SQL OPEN c1 ; CHECKERR( "OPEN CURSOR" ) ; EXEC SQL FETCH c1 USING DESCRIPTOR :*client_sqlda ; CHECKERR( "OPEN CURSOR" ) ; display_col_titles( client_sqlda ) ; i = 0 ; while ( SQLCODE == 0 ) { display_da( client_sqlda ) ; EXEC SQL FETCH c1 USING DESCRIPTOR :*client_sqlda ; i++ ; } EXEC SQL CLOSE c1 ; CHECKERR( "CLOSE CURSOR" ) ; printf( "\n %d record(s) selected.\n\n", i ) ; memset( ( void * ) server_sqlda->sqlvar[0].sqldata, 0, 15 ) ; strcpy( ( char * ) server_sqlda->sqlvar[0].sqldata, "DELETE" ) ; EXEC SQL CALL :procname USING DESCRIPTOR :*server_sqlda ; CHECKERR( "CALL DELETE WITH SQLDA" ) ; printf( "Server Procedure Complete.\n" ) ; printf( "New record(s) deleted.\n\n" ) ; EXEC SQL COMMIT ; CHECKERR( "COMMIT DELETE" ) ; EXEC SQL OPEN c1 ; CHECKERR( "OPEN CURSOR" ) ; EXEC SQL FETCH c1 USING DESCRIPTOR :*client_sqlda ; CHECKERR( "OPEN CURSOR" ) ; display_col_titles( client_sqlda ) ; i = 0 ; while ( SQLCODE == 0 ) { display_da( client_sqlda ) ; EXEC SQL FETCH c1 USING DESCRIPTOR :*client_sqlda ; i++ ; } EXEC SQL CLOSE c1 ; CHECKERR( "CLOSE CURSOR" ) ; printf( "\n %d record(s) selected.\n\n", i ) ; free_da( client_sqlda ) ; free( server_sqlda ) ; EXEC SQL CONNECT RESET ; CHECKERR( "CONNECT RESET" ) ; return( 0 ) ; }