Using SQLGetDiagRec and SQLGetDiagField
Applications retrieve individual diagnostic fields by calling SQLGetDiagField and specifying the field to retrieve. Certain diagnostic fields do not have
any meaning for certain types of handles. For a list of diagnostic fields and
their meaning, see the SQLGetDiagField function description.
Applications retrieve the SQLSTATE, native error code, and diagnostic message
in a single call by calling SQLGetDiagRec; SQLGetDiagRec cannot be used to retrieve information from the header record.
For example, the following code prompts the user for an SQL statement and
executes it. If any diagnostic information was returned, it calls SQLGetDiagField to get the number of status records and SQLGetDiagRec to get the SQLSTATE, native error code, and diagnostic message from those
records.
SQLINTEGER NativeError;
SQLSMALLINT i, MsgLen
SQLRETURN rc1, rc2;
SQLHSTMT hstmt;
// Prompt the user for an SQL statement.
GetSQLStmt(SQLStmt);
// Execute the SQL statement and return any errors or warnings.
rc1 = SQLExecDirect(hstmt, SQLStmt, SQL_NTS);
if ((rc1 == SQL_SUCCESS_WITH_INFO) || (rc1 == SQL_ERROR)) {
// Get the status records.
i = 1;
while ((rc2 = SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, i, SqlState,
&NativeError,
Msg, sizeof(Msg), &MsgLen)) != SQL_NO_DATA) {
DisplayError(SqlState,NativeError,Msg,MsgLen);
i++;
}
}
if ((rc1 == SQL_SUCCESS) || (rc1 == SQL_SUCCESS_WITH_INFO)) {
// Process statement results, if any.
}