/****************************************************************************** ** ** Source File Name = outsrv.sqC 1.1 ** ** Licensed Materials - Property of IBM ** ** (C) COPYRIGHT International Business Machines Corp. 1995, 1997 ** All Rights Reserved. ** ** US Government Users Restricted Rights - Use, duplication or ** disclosure restricted by GSA ADP Schedule Contract with IBM Corp. ** ** ** PURPOSE: This sample program demonstrates stored procedures. ** ** There are two parts to this program: ** - the outcli executable (placed on the client) ** - the outsrv library (placed on the server) ** ** Refer to the outcli.sqC program for more details on how ** this program is invoked as the outsrv routine ** in the outsrv library by the EXEC SQL CALL statement. ** ** The outsrv routine will obtain the median salary of ** employees in the "staff" table of the "sample" database. ** This value will be placed in the input/output SQLDA and ** returned to the outcli routine. The outcli routine will ** then print out the median salary. ** ** EXTERNAL DEPENDENCIES : ** - Existing database for precompile purposes. ** - Precompile with the SQL precompiler (PREP in DB2) ** - Binding to a database (BIND in DB2) ** - Compiling and linking 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 <memory.h> #include <string.h> #include <sqlenv.h> /* :rk.1:erk. */ #include <sql.h> #include <sqlda.h> #ifdef __cplusplus extern "C" #endif SQL_API_RC SQL_API_FN outsrv ( /* :rk.2:erk. */ void *reserved1, void *reserved2, struct sqlda *inout_sqlda, struct sqlca *ca) { /* Declare a local SQLCA */ EXEC SQL INCLUDE SQLCA; /* Declare Host Variables */ EXEC SQL BEGIN DECLARE SECTION; short num_records; char stmt[512]; EXEC SQL END DECLARE SECTION; /* Declare Miscellaneous Variables */ int counter = 0; EXEC SQL WHENEVER SQLERROR GOTO error_exit; EXEC SQL WHENEVER SQLWARNING CONTINUE; EXEC SQL DECLARE c1 CURSOR FOR s1; /* Prepare a Statement to Obtain and Order all Salaries */ strcpy( stmt, "SELECT salary FROM STAFF ORDER BY salary" ); EXEC SQL PREPARE s1 FROM :stmt; /*Determine the Total Number of Records */ EXEC SQL SELECT COUNT(*) INTO :num_records FROM STAFF; /* :rk.3:erk. */ /* Fetch Salaries until the Median Salary is Obtained */ EXEC SQL OPEN c1; while ( counter++ < num_records/2 + 1 ) /* :rk.4:erk. */ EXEC SQL FETCH c1 USING DESCRIPTOR :*inout_sqlda; EXEC SQL CLOSE c1; EXEC SQL COMMIT; /* Return the SQLCA to the Calling Program */ /* :rk.5:erk. */ memcpy( ca, &sqlca, sizeof( struct sqlca ) ); return(SQLZ_DISCONNECT_PROC); error_exit: /* An Error has occurred -- ROLLBACK and return to Calling Program */ EXEC SQL WHENEVER SQLERROR CONTINUE; memcpy( ca, &sqlca, sizeof( struct sqlca ) ); EXEC SQL ROLLBACK; return(SQLZ_DISCONNECT_PROC); }