The following example demonstrates an application that connects to two data sources, and executes both embedded SQL and dynamic SQL using DB2 CLI.
/* From CLI sample mixed.sqc */
/* ... */
/* allocate an environment handle */
SQLAllocEnv(&henv);
/* Connect to first data source */
DBconnect(henv, &hdbc[0]);
/* Connect to second data source */
DBconnect(henv, &hdbc[1]);
/********* Start Processing Step *************************/
/* NOTE: at this point there are two active connections */
/* set current connection to the first database */
if ( (rc = SQLSetConnection(hdbc[0])) != SQL_SUCCESS )
printf("Error setting connection 1\n");
/* call function that contains embedded SQL */
if ((rc = Create_Tab() ) != 0)
printf("Error Creating Table on 1st connection, RC=%d\n", rc);
/* Commit transation on connection 1 */
SQLTransact(henv, hdbc[0], SQL_COMMIT);
/* set current connection to the second database */
if ( (rc = SQLSetConnection(hdbc[1])) != SQL_SUCCESS )
printf("Error setting connection 2\n");
/* call function that contains embedded SQL */
if ((rc = Create_Tab() ) != 0)
printf("Error Creating Table on 2nd connection, RC=%d\n", rc);
/* Commit transation on connection 2 */
SQLTransact(henv, hdbc[1], SQL_COMMIT);
/* Pause to allow the existance of the tables to be verified. */
printf("Tables created, hit Return to continue\n");
getchar();
SQLSetConnection(hdbc[0]);
if (( rc = Drop_Tab() ) != 0)
printf("Error dropping Table on 1st connection, RC=%d\n", rc);
/* Commit transation on connection 1 */
SQLTransact(henv, hdbc[0], SQL_COMMIT);
SQLSetConnection(hdbc[1]);
if (( rc = Drop_Tab() ) != 0)
printf("Error dropping Table on 2nd connection, RC=%d\n", rc);
/* Commit transation on connection 2 */
SQLTransact(henv, hdbc[1], SQL_COMMIT);
printf("Tables dropped\n");
/********* End Processing Step ***************************/
/* ... */
/************* Embedded SQL Functions *******************************
** This would normally be a seperate file to avoid having to *
** keep precompiling the embedded file in order to compile the DB2 CLI *
** section. *
************************************************************************/
#include "sql.h"
#include "sqlenv.h"
EXEC SQL INCLUDE SQLCA;
int
Create_Tab( )
{
EXEC SQL CREATE TABLE mixedup
(ID INTEGER, NAME CHAR(10));
return( SQLCODE);
}
int
Drop_Tab( )
{
EXEC SQL DROP TABLE mixedup;
return( SQLCODE);
}