I've got a script that does a bunch of SQL inserts but I've been trying to add a section that assigns the returned value of a select query into a variable.
The code I've been using to do the inserts is:
if (SQL_SUCCESS != SQLExecDirect(sqlstatementhandle, (SQLCHAR*)"BULK INSERT mytable FROM 'C:/dir/myfile.csv' WITH (FIRSTROW = 1, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n');", SQL_NTS)) {
show_error(SQL_HANDLE_STMT, sqlstatementhandle);
That all works fine, but I can't figure out how to use the output of a query. The output will be a single int value, which I'd like to assign to an int variable.
Apologies if this is something that's blindingly obvious.
EDIT
Based on the answer below, the following has now worked for me. Thanks!
SQLRETURN retcode;
SQLHSTMT hstmt; // I use my own stmnthndl(sqlstatementhandle) below, this line left in for demonstration
retcode = SQLExecDirect(sqlstatementhandle, (SQLCHAR*)"SELECT count(*) FROM mytable;",SQL_NTS);
SQLINTEGER sCustID;
SQLLEN cbCustID;
if (retcode == SQL_SUCCESS) {
while (TRUE) {
retcode = SQLFetch(sqlstatementhandle);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
// get the first column
SQLGetData(sqlstatementhandle, 1, SQL_C_ULONG, &sCustID, 0, &cbCustID);
//You can now print it
cout << "CustID:" << sCustID;
}
else {
break;
}
}
}