[unixODBC-support] SQLGetDiagRec bug

Ian Ashley iashley at picdar.com
Tue Nov 7 14:30:07 GMT 2006


Dear Nick and friends,

I think that a bug has been introduced to SQLGetDiagRec somewhere between
the 2.2.10 and 2.2.12 releases. All the error messages seem to get returned
twice.

The problem is demonstrated by modifying isql.c as shown below that and then
trying to execute an invalid statement. For example

select invalid_column from invalid_table

produces

[S0002][FreeTDS][SQL Server]Invalid object name 'invalid_table'.
[][FreeTDS][SQL Server]Statement(s) could not be prepared.
[S0002][FreeTDS][SQL Server]Invalid object name 'invalid_table'.
[][FreeTDS][SQL Server]Statement(s) could not be prepared.
[ISQL]ERROR: Could not SQLExecute

static int DumpODBCLog( SQLHENV hEnv, SQLHDBC hDbc, SQLHSTMT hStmt )
{
    SQLCHAR        szError[501];
    SQLCHAR        szSqlState[10];
    SQLINTEGER  nNativeError;
    SQLSMALLINT    nErrorMsg;

#if 1
    if ( hStmt )
    {
        SQLSMALLINT recNo = 1;
        while ( SQLGetDiagRec( SQL_HANDLE_STMT, hStmt, recNo++, szSqlState,
&nNativeError, szError, 500, &nErrorMsg ) == SQL_SUCCESS )
        {
            printf( "[%s]%s\n", szSqlState, szError );
        }
    }

    if ( hDbc )
    {
        SQLSMALLINT recNo = 1;
        while ( SQLGetDiagRec( SQL_HANDLE_DBC, hDbc, recNo++, szSqlState,
&nNativeError, szError, 500, &nErrorMsg ) == SQL_SUCCESS )
        {
            printf( "[%s]%s\n", szSqlState, szError );
        }
    }

    if ( hEnv )
    {
        SQLSMALLINT recNo = 1;
        while ( SQLGetDiagRec( SQL_HANDLE_ENV, hEnv, recNo++, szSqlState,
&nNativeError, szError, 500, &nErrorMsg ) == SQL_SUCCESS )
        {
            printf( "[%s]%s\n", szSqlState, szError );
        }
    }
#else
    if ( hStmt )
    {
        while ( SQLError( hEnv, hDbc, hStmt, szSqlState, &nNativeError,
szError, 500, &nErrorMsg ) == SQL_SUCCESS )
        {
            printf( "[%s]%s\n", szSqlState, szError );
        }
    }

    if ( hDbc )
    {
        while ( SQLError( hEnv, hDbc, 0, szSqlState, &nNativeError, szError,
500, &nErrorMsg ) == SQL_SUCCESS )
        {
            printf( "[%s]%s\n", szSqlState, szError );
        }
    }

    if ( hEnv )
    {
        while ( SQLError( hEnv, 0, 0, szSqlState, &nNativeError, szError,
500, &nErrorMsg ) == SQL_SUCCESS )
        {
            printf( "[%s]%s\n", szSqlState, szError );
        }
    }
#endif

    return 1;
}

Cheers,
Ian

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.unixodbc.org/pipermail/unixodbc-support/attachments/20061107/4f191390/attachment.html>


More information about the unixODBC-support mailing list