#include <windows.h>
#include <stdio.h>
#include <sql.h>
#include <sqlext.h>
void main()
{
HENV henv;
HDBC hdbc;
HSTMT hstmt;
SQLRETURN sr;
SQLCHAR* theDiagState = new SQLCHAR[50];
SQLINTEGER theNativeState;
SQLCHAR* theMessageText = new SQLCHAR[255];
SQLSMALLINT iOutputNo;
long param1 = 0;
long param2 = 0;
long param3 = 0;
SQLINTEGER cbValue1 = sizeof(long);
SQLINTEGER cbValue2= sizeof(long);
SQLINTEGER cbValue3= sizeof(long);
SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, & henv );
sr = SQLSetEnvAttr( henv, SQL_ATTR_ODBC_VERSION, ( void * ) SQL_OV_ODBC3, 0 );
sr = SQLAllocHandle( SQL_HANDLE_DBC, henv, & hdbc );
//Please note that the DSN name is LocalPubs here. Chenge the DSN name UserID
//and Password here.
sr = SQLConnect( hdbc, ( unsigned char * ) "LocalPubs", SQL_NTS,
( unsigned char * ) "sa", SQL_NTS,
( unsigned char * ) "", SQL_NTS );
sr = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, & hstmt );
sr = SQLSetStmtAttr( hstmt,
SQL_ATTR_CURSOR_TYPE,
( void * ) SQL_CURSOR_DYNAMIC,
SQL_IS_INTEGER );
sr= SQLBindParameter( hstmt,
1,
SQL_PARAM_OUTPUT,
SQL_C_LONG,
SQL_INTEGER,
sizeof( long ),
0,
& param1,
sizeof( long ),
& cbValue1 );
sr = SQLBindParameter( hstmt,
2,
SQL_PARAM_INPUT,
SQL_C_LONG,
SQL_INTEGER,
sizeof( long ),
0,
& param2,
sizeof( long ),
& cbValue2 );
sr = SQLBindParameter( hstmt,
3,
SQL_PARAM_OUTPUT,
SQL_C_LONG,
SQL_INTEGER,
sizeof( long ),
0,
& param3,
sizeof( long ),
& cbValue3 );
sr = SQLExecDirect( hstmt,
( unsigned char * ) "{ ? = call sp_myproc(?, ?)}", SQL_NTS );
if (sr != SQL_SUCCESS)
{
//With this bug you will get an error message. Check this message in theMessageText.
SQLGetDiagRec(SQL_HANDLE_STMT,hstmt,1,theDiagState,&theNativeState,theMessageText,100,&iOutputNo);
}
//With the fix, you should get the return value after calling this SQLMoreResult
sr = SQLMoreResults( hstmt );
//Free allocated memory and disconnect
SQLFreeStmt( hstmt, SQL_CLOSE );
SQLFreeStmt( hstmt, SQL_DROP );
SQLDisconnect( hdbc );
SQLFreeHandle( SQL_HANDLE_DBC, hdbc );
SQLFreeHandle( SQL_HANDLE_ENV, henv );
delete theMessageText;
delete theDiagState;
}