Executing statements directly

The SQLExecDirect function prepares and executes a SQL statement. Optionally, the statement can include parameters.

The following code fragment illustrates how to execute a statement without parameters. The SQLExecDirect function takes a statement handle, a SQL string, and a length or termination indicator, which in this case is a null-terminated string indicator.

StepsTo execute a SQL statement in an ODBC application

  1. Allocate a handle for the statement using SQLAllocHandle.

    For example, the following statement allocates a SQL_HANDLE_STMT handle with the name “stmt,” on a connection with a handle named “dbc”:

    SQLAllocHandle( SQL_HANDLE_STMT, dbc, &stmt );
    
  2. Call the SQLExecDirect function to execute the statement.

    For example, the following lines declare a statement and execute it:

    SQLCHAR *deletestmt =
       "DELETE FROM department WHERE dept_id = 201";
    SQLExecDirect( stmt, deletestmt, SQL_NTS) ;
    

For more information, see SQLExecDirect in the Microsoft ODBC Programmer's Reference.