Execute ODBC functions

Use these handle types to execute ODBC functions.

SQLAllocHandle with SQL_HANDLE_STMT as the handle type
  • Allocates memory for information about an SQL statement.
    • Application must request a statement handle prior to submitting SQL statements.
    • Variable type HSTMT.

      In C, this statement is coded:

      HSTMT hstmt;
      
      rc = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
SQLExecDirect
  • Executes a preparable statement.
  • Fastest way to submit an SQL string for one time execution.
  • If rc is not equal to SQL_SUCCESS, the SQLGetDiagRec API can be used to find the cause of the error condition.

    In C, this statement is coded:

    SQLCHAR stmt[ ] = "CREATE TABLE NAMEID (ID INTEGER, NAME VARCHAR(50))";
    
    rc = SQLExecDirect(hstmt, stmt, SQL_NTS);
  • Return code
    • SQL_SUCCESS
    • SQL_SUCCESS_WITH_INFO
    • SQL_ERROR
    • SQL_INVALID_HANDLE
SQLGetDiagRec
To retrieve error information for an error on a statement:

In C, this statement is coded:

SQLSMALLINT i = 1, cbErrorMsg ;
SQLCHAR    szSQLState[6], szErrorMsg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER nativeError; 

rc = SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, i, szSQLState, &nativeError, szErrorMsg, 
                                  SQL_MAX_MESSAGE_LENGTH, &cbErrorMsg);
  • szSQLState
    • 5 character string
    • 00000 = success
    • 01004 = data truncated
    • 07001 = wrong number of parameters
      Note: The previous items are only several of many possible SQL states.
  • fNativeError - specific to data source
  • szErrorMsg - Error Message text