Contents Index Executing SQL statements Executing statements with bound parameters

ASA Programming Guide
  ODBC Programming
    Executing SQL statements

Executing statements directly


The SQLExecDirect function prepares and executes a SQL statement. The statement may be optionally 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.

The procedure described in this section is straightforward but inflexible. The application cannot take any input from the user to modify the statement. For a more flexible method of constructing statements, see Executing statements with bound parameters.

To execute a SQL statement in an ODBC application

  1. Allocate a handle for the statement using SQLAllocHandle.

    For example, the following statement allocates a handle of type SQL_HANDLE_STMT with name stmt, on a connection with handle 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. The declaration of deletestmt would usually occur at the beginning of the function:

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

For a complete sample with error checking, see Samples\ASA\ODBCExecute\odbcexecute.cpp.

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


Contents Index Executing SQL statements Executing statements with bound parameters