Visual C++ Example
The following example uses Visual C++ to retrieve errors:
void DisplayError(HRESULT hr)
{
HRESULT hrError;
IErrorInfo *pIErrorInfo; // interface pointer
IErrorRecords *pIErrorRecords; // interface pointer
ISQLErrorInfo *pISQLErrorInfo; // interface pointer
BSTR bstrErrorDesc; // Buffer for message
ERRORINFO ErrorInfo; // Error structure
BSTR bstrSQLState; // Buffer for state
long lNativeError; // Native error
unsigned long cRecordCount;
// Checking if return code is an error
if (hr != S_OK)
{
// Getting the Error message text
hrError = GetErrorInfo(0, &pIErrorInfo);
hrError = pIErrorInfo->GetDescription(&bstrErrorDesc);
hrError = pIErrorInfo->QueryInterface(IID_IErrorRecords, (void **)&pIErrorRecords);
// Getting information on each Error record
hrError = pIErrorRecords->GetRecordCount(&cRecordCount);
//Going to retrieve errors generated by Teradata Database
for (int i = 0; i < cRecordCount; i++)
{
hrError = pIErrorRecords->GetCustomErrorObject(i,
IID_ISQLErrorInfo,
(IUnknown **)&pISQLErrorInfo);
hrError = pIErrorRecords->GetBasicErrorInfo(i, &ErrorInfo);
// Going to get the SQL STATE and Native Error Code
// from the error record
hrError = pISQLErrorInfo->GetSQLInfo(&bstrSQLState,
&NativeError);
if (bstrSQLState)
wprintf(L"\nErrorRecord: HResult: 0x%08x\nDescription: %s\n”
L”SQLErrorInfo: %s\n Native Error Code: %d\n",
ErrorInfo.hrError,
bstrErrorDesc,
bstrSQLInfo
NativeError);
else
wprintf(L"\nErrorRecord: HResult: 0x%08x\nDescription: %s\n”
ErrorInfo.hrError,
bstrErrorDesc);
}
// Releasing resources
SysFreeString(BstrErrorDesc);
If (!bstrSQLState)
SysFreeString(bstrSQLState);
pIErrorInfo->Release();
pIErrorRecords->Release();
pISQLErrorInfo->Release();
}
}