Executing statements directly

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

 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 Departments WHERE DepartmentID = 201";
    SQLExecDirect( stmt, deletestmt, SQL_NTS) ;

For a complete sample with error checking, see %SQLANYSAMP12%\SQLAnywhere\ODBCExecute\odbcexecute.cpp.

For more information about SQLExecDirect, see SQLExecDirect in the Microsoft ODBC API Reference at [external link] http://msdn.microsoft.com/en-us/library/ms713611.aspx.