Specification: | DB2 CLI 2.1 | ODBC 2.0 |
SQLBindParameter() is used to associate (bind) parameter markers in an SQL statement to application variables, for all C data types. In this case data is transferred from the application to the DBMS when SQLExecute() or SQLExecDirect() is called. Data conversion might occur when the data is transferred.
Syntax
SQLRETURN SQL_API SQLBindParameter( SQLHSTMT StatementHandle, /* hstmt */ SQLUSMALLINT ParameterNumber, /* ipar */ SQLSMALLINT InputOutputType, /* fParamType */ SQLSMALLINT ValueType, /* fCType */ SQLSMALLINT ParameterType, /* fSqlType */ SQLUINTEGER ColumnSize, /* cbColDef */ SQLSMALLINT DecimalDigits, /* ibScale */ SQLPOINTER ParameterValuePtr,/* rgbValue */ SQLINTEGER BufferLength, /* cbValueMax */ SQLINTEGER *FAR StrLen_or_IndPtr);/* pcbValue */
Function arguments
Table 34. SQLBindParameter arguments
Data type | Argument | Use | Description |
---|---|---|---|
SQLHSTMT | StatementHandle | input | Statement handle. |
SQLUSMALLINT | ParameterNumber | input | Parameter marker number, ordered sequentially left to right, starting at one. |
SQLSMALLINT | InputOutputType | input | The type of parameter. The supported type is:
|
SQLSMALLINT | ValueType | input | C data type of the parameter. The following types are
supported:
Specifying SQL_C_DEFAULT causes data to be transferred from its default C data type to the type indicated in ParameterType. |
SQLSMALLINT | ParameterType | input | SQL data type of the parameter. The supported types are:
|
SQLUINTEGER | ColumnSize | input | Precision of the corresponding parameter marker.
|
SQLSMALLINT | DecimalDigits | input | Scale of the corresponding parameter if ParameterType is SQL_DECIMAL. |
SQLPOINTER | ParameterValuePtr | input (deferred), output (deferred), or both |
|
SQLINTEGER | BufferLength | input | For character and binary data, BufferLength specifies the length of the ParameterValuePtr buffer. For non-character and non-binary data, this argument is ignored and the length of the ParameterValuePtr buffer is assumed to be the length associated with the C data type. For output parameters, BufferLength is used to determine whether to truncate data. |
SQLINTEGER * | StrLen_or_IndPtr | input (deferred), output (deferred), or both |
|
Usage
A parameter marker is represented by a ? character in an SQL statement and is used to indicate a position in the statement where an application-supplied value is to be substituted when the statement is executed. This value can be obtained from an application variable. SQLBindParameter() is used to bind the application storage area to the parameter marker.
The application must bind a variable to each parameter marker in the SQL statement before executing the SQL statement. For this function, ParameterValuePtr and StrLen_or_IndPtr are deferred arguments. The storage locations must be valid and contain input data values when the statement is executed. This means that either the SQLExecDirect() or SQLExecute() call must be kept in the same procedure scope as the SQLBindParameter() calls, or these storage locations must be dynamically allocated or declared statically or globally.
Parameter markers are referenced by number (ColumnNumber) and are numbered sequentially from left to right, starting at one.
All parameters bound by this function remain in effect until one of the following functions is called:
After the SQL statement is executed and the results processed, the application might want to reuse the statement handle to execute a different SQL statement. If the parameter marker specifications are different (number of parameters, length, or type) then SQLFreeStmt() must be called with SQL_RESET_PARAMS to reset or clear the parameter bindings.
The C buffer data type given by ValueType must be compatible with the SQL data type indicated by ParameterType, or an error occurs.
Because the data in the variables referenced by ParameterValuePtr and StrLen_or_IndPtr is not verified until the statement is executed, data content or format errors are not detected or reported until SQLExecute() or SQLExecDirect() is called.
For this function, ParameterValuePtr and StrLen_or_IndPtr are deferred arguments. In the case where InputOutputType is set to SQL_PARAM_INPUT, the storage locations must be valid and contain input data values when the statement is executed. This means that either the SQLExecDirect() or SQLExecute() call must be kept in the same procedure scope as the SQLBindParameter() calls, or these storage locations must be dynamically allocated or declared statically or globally.
DB2 Everyplace supports SQL_PARAM_INPUT, SQL_PARAM_INPUT_OUTPUT, and SQL_PARAM_OUTPUT. DB2 Everyplace does not support SQLPutData(), so you should not store the parameter value in the ParameterValuePtr buffer.
For character and binary C data, the BufferLength argument specifies the length of the ParameterValuePtr buffer. For all other types of C data, the BufferLength argument is ignored.
Return codes
Diagnostics
Table 35. SQLBindParameter SQLSTATEs
SQLSTATE | Description | Explanation |
---|---|---|
07006 | Invalid conversion. | The conversion from the data value identified by the ValueType argument to the data type identified by the ParameterType argument is not a meaningful conversion. (For example, conversion from SQL_C_DATE to SQL_DOUBLE.) |
40003 08S01 | Communication link failure. | The communication link between the application and data source failed before the function completed. |
58004 | Unexpected system failure. | Unrecoverable system error. |
HY001 | Memory allocation failure. | DB2 CLI is unable to allocate memory required to support execution or completion of the function. |
HY003 | Program type out of range. | The value specified by the argument ParameterNumber is not a valid data type or SQL_C_DEFAULT. |
HY004 | SQL data type out of range. | The value specified for the argument ParameterType is not a valid SQL data type. |
HY009 | Invalid argument value. | The argument ParameterValuePtr is a null pointer, and the argument StrLen_or_IndPtr is a null pointer, and InputOutputType is not SQL_PARAM_OUTPUT. |
HY013 | Unexpected memory handling error. | DB2 CLI is unable to access memory required to support execution or completion of the function. |
HY090 | Invalid string or buffer length. | The value specified for the argument BufferLength is less than 0. |
HY093 | Invalid parameter number. | The value specified for the argument ValueType is less than one or greater than the maximum number of parameters supported by the server. |
HY094 | Invalid scale value. | The value specified for ParameterType is either SQL_DECIMAL or SQL_NUMERIC, and the value specified for DecimalDigits is less than 0 or greater than the value for the argument ParamDef (precision). |
HY104 | Invalid precision value. | The value specified for ParameterType is either SQL_DECIMAL or SQL_NUMERIC, and the value specified for ParamDef is less than one. |
HY105 | Invalid parameter type. | InputOutputType is not SQL_PARAM_INPUT. |
HYC00 | Driver not capable. | DB2 CLI or data source does not support the conversion specified by the
combination of the value specified for the argument ValueType and the
value specified for the argument ParameterType.
The value specified for the argument ParameterType is not supported by either DB2 CLI or the data source. |
Related reference