Executing a SQL Statement in an ODBC Application

Use the SQLExecDirect function to prepare and execute a SQL statement.

Optionally, the statement can include parameters. The SQLExecDirect function takes a statement handle, a SQL string, and a length or termination indicator, which in this example below is a null-terminated string indicator.

This code fragment illustrates how to execute a statement without parameters.

  1. Allocate a handle for the statement using SQLAllocHandle.

    For example, this 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, these lines declare a statement and execute it:

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

See SQLExecDirect in the Microsoft ODBC Programmer's Reference.