简体   繁体   中英

SQL Server - Calling Stored Procedures using C++ ODBC

I'm trying to call a stored procedure to insert data into a table and return a value. I'm getting a return code 99 which says SQL_NEED_DATA. I'm using Visual Studio 2008 and SQL Server Native Client 11 driver. Code extract:

rc=SQL_SUCCESS; 
SQLINTEGER exID;
SQLINTEGER cdexFolderName = SQL_NTS;
SQLINTEGER cbexRootPath = SQL_NTS;
SQL_DATE_STRUCT dsOrderDate ;
SQLINTEGER cbOrderDate = 0;
rc=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);

if(rc==SQL_SUCCESS)
rc=SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3, 0); 
if(rc==SQL_SUCCESS)
rc=SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hConn);
rc = SQLDriverConnect (hConn, NULL, (SQLCHAR*) "DSN=TWPDEV;Trusted_Connection=yes;", 
                SQL_NTS, retconstring, 1024, NULL,SQL_DRIVER_NOPROMPT);
rc = SQLAllocHandle(SQL_HANDLE_STMT, hConn, &hStmt);

if (!(rc==0 || rc==1))
show_error(SQL_HANDLE_DBC, hConn);
rc = SQLBindParameter(hStmt, 1, SQL_PARAM_OUTPUT, SQL_C_SLONG, 
                SQL_INTEGER, 0, 0, &exID, 0, &cbexID);
if (!(rc==0 || rc==1))
show_error(SQL_HANDLE_STMT, hStmt); 
rc = SQLBindParameter(hStmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, 
        SQL_LONGVARCHAR, sizeof(dsOrderDate), 0, &dsOrderDate, 0, &cbOrderDate);
if (!(rc==0 || rc==1))
show_error(SQL_HANDLE_STMT, hStmt); 
rc = SQLBindParameter(hStmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, 
                SQL_LONGVARCHAR, 50, 0, (SQLPOINTER) ExtractionFolderName.c_str(), ExtractionFolderName.length(), &cdexFolderName);
if (!(rc==0 || rc==1))
show_error(SQL_HANDLE_STMT, hStmt); 

rc = SQLBindParameter(hStmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, 
                SQL_LONGVARCHAR, 50, 0, (SQLPOINTER) ExtractionRootPath.c_str(), ExtractionRootPath.length(), &cbexRootPath);
if (!(rc==0 || rc==1))
show_error(SQL_HANDLE_STMT, hStmt); 


rc = SQLBindParameter(hStmt, 5, SQL_PARAM_INPUT, SQL_C_SLONG, 
                SQL_INTEGER, 0, 0, (SQLPOINTER) &NoOfItemsExtracted, 0, &cbnumItemsExtracted);
if (!(rc==0 || rc==1))
show_error(SQL_HANDLE_STMT, hStmt); 
rc = SQLBindParameter(hStmt, 6, SQL_PARAM_INPUT, SQL_C_SLONG, 
                SQL_INTEGER, 0, 0, (SQLPOINTER) &Status, 0, &cbstatus);
if (!(rc==0 || rc==1))
show_error(SQL_HANDLE_STMT, hStmt); 
rc = SQLBindParameter(hStmt, 7, SQL_PARAM_INPUT, SQL_C_SLONG, 
                SQL_INTEGER, 0, 0, (SQLPOINTER) &instanceID, 0, NULL);
if (!(rc==0 || rc==1))
show_error(SQL_HANDLE_STMT, hStmt); 

rc = SQLBindParameter(hStmt, 8, SQL_PARAM_INPUT, SQL_C_SLONG, 
                SQL_INTEGER, 0, 0, (SQLPOINTER) &sourceID, 0,NULL);
if (!(rc==0 || rc==1))
show_error(SQL_HANDLE_STMT, hStmt); 

rc = SQLBindParameter(hStmt, 9, SQL_PARAM_INPUT, SQL_C_SLONG, 
                SQL_INTEGER, 0, 0, (SQLPOINTER) &migrationSts, 0, &cbmigStatus);
if (!(rc==0 || rc==1))
show_error(SQL_HANDLE_STMT, hStmt);     

rc = SQLPrepare (hStmt, (SQLCHAR*) "{? = call dbo.procInsertExtraction(?,?,?,?,?,?,?,?)}", SQL_NTS);
if (!(rc==0 || rc==1))
show_error(SQL_HANDLE_STMT, hStmt);

rc = SQLExecute(hStmt);
        if (!(rc==0 || rc==1))
            show_error(SQL_HANDLE_STMT, hStmt);

All the statements until SQL Execute are returning rc=0. I'm tried connecting to db and executed a simple SQL statement to insert data, it was successful. But failing in case of running a procedure. Please help me in resolving this.

I believe that this could be happening because of your second parameter binding:

rc = SQLBindParameter(hStmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, 
    SQL_LONGVARCHAR, sizeof(dsOrderDate), 0, &dsOrderDate, 0, &cbOrderDate);

To understand this better, I suggest looking at the ODBC Help guide from the Microsoft Data Access SDK, but basically, this particular line of code appears to be meeting the conditions required for specifying that you plan on sending the data for this parameter via SQLPutData() calls.

The key components in this instance are that the parameterType argument is SQL_LONGVARCHAR and the code is specifying a pointer to a value of (0).

If you are not intending to send the parameter data via SQLPutData() calls, try setting cbOrderDate=SQL_NTS; at the top of your code instead of setting it to a value of (0).

Tony Hall

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM