Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - Programming » SQL Anywhere ODBC API » Executing SQL statements

Executing statements with bound parameters Next Page

Executing prepared statements


Prepared statements provide performance advantages for statements that are used repeatedly. ODBC provides a full set of functions for using prepared statements.

For an introduction to prepared statements, see Preparing statements.

To execute a prepared SQL statement
  1. Prepare the statement using SQLPrepare.

    For example, the following code fragment illustrates how to prepare an INSERT statement:

    SQLRETURN   retcode;
    SQLHSTMT    stmt;
    retcode = SQLPrepare( stmt,
                "INSERT INTO Departments
                 ( DepartmentID, DepartmentName, DepartmentHeadID )
                 VALUES (?, ?, ?,)",
              SQL_NTS);

    In this example:

  2. Set statement parameter values using SQLBindParameter.

    For example, the following function call sets the value of the DepartmentID variable:

    SQLBindParameter( stmt,
                     1,
                     SQL_PARAM_INPUT,
                     SQL_C_SSHORT,
                     SQL_INTEGER,
                     0,
                     0,
                     &sDeptID,
                     0,
                     &cbDeptID);

    In this example:

  3. Bind the other two parameters and assign values to sDeptId.

  4. Execute the statement:

    retcode = SQLExecute( stmt);

    Steps 2 to 4 can be carried out multiple times.

  5. Drop the statement.

    Dropping the statement frees resources associated with the statement itself. You drop statements using SQLFreeHandle.

For a complete sample, including error checking, see samples-dir\SQLAnywhere\ODBCPrepare\odbcprepare.cpp.

For more information about SQLPrepare, see [external link] SQLPrepare in the Microsoft ODBC Programmer's Reference.