Prepared Execution
SQLUINTEGER PartID;
SQLINTEGER PartIDInd = 0, PriceInd = 0;
// Prepare a statement to update salaries in the Employees table.
SQLPrepare(hstmt, "UPDATE Parts SET Price = ? WHERE PartID = ?", SQL_NTS);
// Bind Price to the parameter for the Price column and PartID to
// the parameter for the PartID column.
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_REAL, 7, 0,
&Price, 0, &PriceInd);
SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 10, 0,
&PartID, 0, &PartIDInd);
// Repeatedly execute the statement.
while (GetPrice(&PartID, &Price)) {
SQLExecute(hstmt);
}
The application can retrieve the metadata for the result set after the
statement is prepared and before it is executed. However, returning metadata for
prepared, unexecuted statements is expensive for some drivers and should be avoided
by interoperable applications if possible. For more information, see
Important Committing or rolling back a transaction, either by explicitly calling SQLEndTran or by working in autocommit mode, causes some data sources to delete the
access plans for all statements on a connection. For more information, see the
SQL_CURSOR_COMMIT_BEHAVIOR and SQL_CURSOR_ROLLBACK_BEHAVIOR options in the SQLGetInfo function description.
To prepare and execute a statement, the application:
1 Calls SQLPrepare and passes it a string containing the SQL statement.
2 Sets the values of any parameters. Parameters can actually be set before or
after preparing the statement. For more information, see
3 Calls SQLExecute and does any additional processing that is necessary, such as fetching data.
4 Repeats steps 2 and 3 as necessary.
When SQLPrepare is called, the driver:
1 Modifies the SQL statement to use the data source
2 Sends the statement to the data source for preparation.
3 Stores the returned access plan identifier for later execution (if the
preparation succeeded) or returns any errors (if the preparation failed). Errors
include syntactic errors such as SQLSTATE 42000 (Syntax error or access violation)
and semantic errors such as SQLSTATE 42S02 (Base table or view not found).
Note Some drivers do not return errors at this point, but instead return them
when the statement is executed or when catalog functions are called. Thus, SQLPrepare might appear to have succeeded when in fact it has failed.
When SQLExecute is called, the driver:
1 Retrieves the current parameter values and converts them as necessary. For
more information, see
2 Sends the access plan identifier and converted parameter values to the data
source.
3 Returns any errors. These are generally run-time errors such as SQLSTATE 24000
(Invalid cursor state). However, some drivers return syntactic and semantic
errors at this point.
If the data source does not support statement preparation, the driver must
emulate it to the extent possible. For example, the driver might do nothing when SQLPrepare is called, then perform direct execution of the statement when SQLExecute is called.
If the data source supports syntax checking without execution, the driver
might submit the statement for checking when SQLPrepare is called and submit the statement for execution when SQLExecute is called.
If the driver cannot emulate statement preparation, it stores the statement
when SQLPrepare is called and submits it for execution when SQLExecute is called.
Because emulated statement preparation is not perfect, SQLExecute can return any errors normally returned by SQLPrepare.