SQLBindParameter() - Bind a parameter marker
to a buffer or LOB locator
SQLBindParameter() binds parameter markers
to application variables and extends the capability of the SQLSetParam() function.
ODBC specifications for SQLBindParameter()
| ODBC specification level | In X/Open CLI CAE specification? | In ISO CLI specification? |
|---|---|---|
| 2.0 | No | No |
Syntax
For 31-bit applications, use the following syntax:
SQLRETURN SQL_API SQLBindParameter(
SQLHSTMT hstmt,
SQLUSMALLINT ipar,
SQLSMALLINT fParamType,
SQLSMALLINT fCType,
SQLSMALLINT fSqlType,
SQLUINTEGER cbColDef,
SQLSMALLINT ibScale,
SQLPOINTER rgbValue,
SQLINTEGER cbValueMax,
SQLINTEGER FAR *pcbValue);For 64-bit applications, use the following syntax:
SQLRETURN SQL_API SQLBindParameter(
SQLHSTMT hstmt,
SQLUSMALLINT ipar,
SQLSMALLINT fParamType,
SQLSMALLINT fCType,
SQLSMALLINT fSqlType,
SQLULEN cbColDef,
SQLSMALLINT ibScale,
SQLPOINTER rgbValue,
SQLLEN cbValueMax,
SQLLEN FAR *pcbValue); Function arguments
The following table lists the data type, use, and description for each argument in this function.
| Data type | Argument | Use | Description |
|---|---|---|---|
| SQLHSTMT | hstmt | input | Specifies the statement handle of the statement you bind. |
| SQLUSMALLINT | ipar | input | Specifies the parameter marker number, which are ordered sequentially left to right, starting at 1. |
| SQLSMALLINT | fParamType | input | Specifies the type of parameter. You can specify
the following types of parameters:
|
| SQLSMALLINT | fCType | input | Specifies the C data type of the parameter. The
following types are supported:
|
| SQLSMALLINT | fSqlType | input | Specifies the SQL data type of the parameter. The
supported types are:
Restriction: SQL_BLOB_LOCATOR, SQL_CLOB_LOCATOR, and SQL_DBCLOB_LOCATOR are application related concepts and do not map to a data type for column definition during a CREATE TABLE. |
| SQLUINTEGER (31-bit) or SQLULEN (64-bit) 1 | cbColDef | input | Specifies
the precision of the corresponding parameter marker. The meaning of
this precision depends on what data type the fSqlType argument
denotes:
|
| SQLSMALLINT | ibScale | input | Specifies the scale of the corresponding parameter
if the fSqlType argument is SQL_DECIMAL
or SQL_NUMERIC. If the fSqlType argument
specifies SQL_TYPE_TIMESTAMP, this is the number of digits to the
right of the decimal point in the character representation of a timestamp
(for example, the scale of yyyy-mm-dd hh:mm:ss.fff is 3). Other than the values for the fSqlType argument that are mentioned here, the ibScale argument is ignored. |
| SQLPOINTER | rgbValue | input (deferred), output (deferred), or input (deferred) and output (deferred) | The following characteristics apply to the rgbValue argument
depending on whether it is an input argument, an output argument,
or both:
|
| SQLINTEGER (31-bit) or SQLLEN (64-bit) 2 | cbValueMax | input | For character and binary data, the cbValueMax argument specifies the size, in bytes, of the buffer that the rgbValue argument indicates. If this buffer is a single element, this value specifies the size of that element. If
this buffer is an array, the value specifies the size of each element
in that array. Call This length is assumed to be the length that is associated with the C data type in these cases. For
output parameters, the cbValueMax argument
is used to determine whether to truncate character or binary output
data. Data is truncated in the following manner:
|
| SQLINTEGER * (31-bit) SQLLEN * (64-bit)2 | pcbValue | input (deferred), output (deferred), or input (deferred) and output (deferred) | The following characteristics apply to the pcbValue argument depending on whether it is an input argument, an output argument, or both:
|
| Continued SQLINTEGER * (31-bit) SQLLEN * (64-bit)2 | pcbValue | input (deferred), output (deferred), or input (deferred) and output (deferred) |
|
Notes:
|
|||
Usage
SQLBindParameter() associates,
or binds, parameter markers in an SQL statement to the following
objects: - All C type application variables or arrays of C type application
variables (storage buffers). For application variables, data is transferred
from your application to the database management system when you call
SQLExecute()orSQLExecDirect(). This transfer converts data from the C type of the application variable to the SQL type that you specify in theSQLBindParameter()call. - SQL LOB type LOB locators. For LOB data types, you transfer a LOB locator value (not the LOB data itself) to the server when you execute an SQL statement.
SQLBindParameter() also binds application
storage to a parameter in a stored procedure CALL statement. In this
type of bind, parameters can be input, output, or both input and output
parameters.
Call SQLBindParameter() to bind
parameter markers to application variables. Parameter markers are
question mark characters (?) that you place in an SQL statement. When
you execute a statement that contains parameter markers, each of these
markers is replaced with the contents of a host variable.
SQLBindParameter() essentially
extends the capability of the SQLSetParam() function
by providing the following functionality: - Can specify whether a parameter is input, output, or both input and output, which is necessary to handle parameters for stored procedures properly.
- Can
specify an array of input parameter values when
SQLSetStmtAttr()is used in conjunction withSQLBindParameter().SQLSetParam()can still be used to bind single element application variables to parameter markers that are not part of a stored procedure CALL statement.
SQLBindParameter() to bind a parameter
marker to one of the following sources: - An application variable.
- A LOB value from the database server (by specifying a LOB locator).
SQLBindParameter(),
the rgbValue argument and the pcbValue argument
are deferred arguments. The storage locations you provide for these
arguments must be valid and contain input data values when you execute
the bound statement. This requirement means that you must follow one of
the following guidelines: - Keep calls to
SQLExecDirect()orSQLExecute()in the same procedure scope as calls toSQLBindParameter(). - Dynamically allocate storage locations that you use for input or output parameters.
- Statically declare storage locations that you use for input or output parameters.
- Globally declare storage locations that you use for input or output parameters.
Binding a parameter marker to a LOB locator: When you bind LOB locators to parameter markers the database server supplies the LOB value. Your application transfers only the LOB locator value across the network.
With LOB locators, you can use SQLGetSubString(), SQLGetPosition(),
or SQLGetLength(). SQLGetSubString() can
return either another locator or the data itself. All locators remain
valid until the end of the transaction in which you create them (even
when the cursor moves to another row), or until you issue the FREE
LOCATOR statement.
Obtaining information about the result
set: You can call SQLBindParameter() before SQLPrepare() if
you know what columns appear in the result set. Otherwise, if you
do not know what columns appear in the result set, you must obtain
column attributes after you prepare your query statement.
You
reference parameter markers by number, which the ipar argument
in SQLBindParameter() represents. Parameter markers
are numbered sequentially from left to right, starting at 1.
Specifying
the parameter type: The fParamType argument
specifies the type of the parameter. All parameters in the SQL statements
that do not call procedures are input parameters. Parameters in stored
procedure calls can be input, input/output, or output parameters.
Even though the Db2 stored procedure
argument convention typically implies that all procedure arguments
are input/output, the application programmer can still choose to specify
the nature of input or output more exactly on the SQLBindParameter() to
follow a more rigorous coding style. When you set the fParamType argument,
consider the following Db2 ODBC
behaviors:
- If an application cannot determine the type of a parameter in a procedure call, set the fParamType argument to SQL_PARAM_INPUT; if the data source returns a value for the parameter, Db2 ODBC discards it.
- If an application has marked a parameter as SQL_PARAM_INPUT_OUTPUT or SQL_PARAM_OUTPUT and the data source does not return a value, Db2 ODBC sets the buffer that the pcbValue argument specifies to SQL_NULL_DATA.
- If an application marks a parameter as SQL_PARAM_OUTPUT, data for the parameter is returned to the application after the CALL statement is processed. If the rgbValue and pcbValue arguments are both null pointers, Db2 ODBC discards the output value. If the data source does not return a value for an output parameter, Db2 ODBC sets the pcbValue buffer to SQL_NULL_DATA.
- When the fParamType argument is set
to SQL_PARAM_INPUT or SQL_PARAM_INPUT_OUTPUT, the storage locations
must be valid and contain input data values when the statement is
executed. Because the rgbValue and pcbValue arguments
are deferred arguments, you must keep either the
SQLExecDirect()or theSQLExecute()call in the same procedure scope as theSQLBindParameter()calls, or the argument values for rgbValue and pcbValue must be dynamically allocated or statically or globally declared.Similarly, if the fParamType argument is set to SQL_PARAM_OUTPUT or SQL_PARAM_INPUT_OUTPUT, the buffers that the rgbValue and pcbValue arguments specify must remain valid until the CALL statement is executed.
SQLBindParameter() binds
remain bound until you perform one of the following actions: - Call
SQLFreeHandle()with the HandleType argument set to SQL_HANDLE_STMT. - Call
SQLFreeStmt()with the fOption argument set to SQL_RESET_PARAMS. - Call
SQLBindParameter()again for the same parameter ipar number.
After an SQL statement is executed, and the results processed,
you 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), you should call SQLFreeStmt() with
SQL_RESET_PARAMS to reset or clear the parameter bindings.
The C buffer data type given by fCType must be compatible with the SQL data type indicated by fSqlType, or an error occurs.
Specifying data-at-execution parameters: An
application can pass the value for a parameter either in the rgbValue buffer
or with one or more calls to SQLPutData(). In calls
to SQLPutData(), these parameters are data-at-execution
parameters. The application informs Db2 ODBC
of a data-at-execution parameter by placing the SQL_DATA_AT_EXEC value
in the pcbValue buffer. It sets the rgbValue input
argument to a 32-bit value which is returned on a subsequent SQLParamData() call
and can be used to identify the parameter position.
Because
the data in the variables referenced by rgbValue and pcbValue is
not verified until the statement is executed, data content or format
errors are not detected or reported until SQLExecute() orSQLExecDirect() is
called.
Allocating
buffers: For character and binary C data, the cbValueMax argument
specifies the length (in bytes) of the rgbValue buffer
if it is a single element; or, if the application calls SQLSetStmtAttr() to
specify multiple values for each parameter, the cbValueMax argument
specifies the length (in bytes) of each element
in the rgbValue array, including the
nul-terminator. If the application specifies multiple values, cbValueMax is
used to determine the location of values in the rgbValue array.
For all other types of C data, the cbValueMax argument
is ignored.
You can pass the value for a parameter with either
the buffer that the rgbValue argument specifies
or one or more calls to SQLPutData(). In calls to SQLPutData(),
these parameters are data-at-execution parameters. The application
informs Db2 ODBC of a data-at-execution
parameter by placing the SQL_DATA_AT_EXEC value in the pcbValue buffer.
It sets the rgbValue input argument to a
32-bit value which is returned on a subsequent SQLParamData() call
and can be used to identify the parameter position.
- SQLSTATE 22001 when the cbColDef argument is less than 40
- SQLSTATE HY104 when the cbColDef argument is greater than 40
SQLBindParameter() is used to bind
an application variable to an output parameter for a stored procedure, Db2 ODBC can provide some performance
enhancement if the rgbValue buffer is placed
consecutively in memory after the pcbValue buffer.
For example: struct { SQLINTEGER pcbValue;
SQLCHAR rgbValue[MAX_BUFFER];
} column;SQLBindParameter() calls):
- The ODBC driver places EBCDIC data into application variables
when both of the following conditions are true:
- CURRENTAPPENSCH = EBCDIC is specified in the initialization file, the CCSID that is specified for the CURRENTAPPENSCH keyword is an EBCDIC CCSID, or the CURRENTAPPENSCH keyword is not specified in the initialization file.
- The fCType argument specifies SQL_C_CHAR
or SQL_C_DBCHAR in the
SQLBindParameter()call.
- The ODBC driver places Unicode UCS-2 data into application variables
when the fCType argument specifies SQL_C_WCHAR
in the
SQLBindParameter()call. - The ODBC driver places Unicode UTF-8 data into application variables
when both of the following conditions are true:
- CURRENTAPPENSCH = UNICODE is specified in the initialization file, or the CCSID that is specified for CURRENTAPPENSCH is a Unicode CCSID (1200, 1208, 13488 or 17584).
- The fCType argument specifies SQL_C_CHAR
in the
SQLBindParameter()call.
- The ODBC driver places ASCII data into application variables when
both of the following conditions are true:
- CURRENTAPPENSCH = ASCII is specified in the initialization file, or the CCSID that is specified for CURRENTAPPENSCH is an ASCII CCSID.
- The fCType argument specifies SQL_C_CHAR
or SQL_C_DBCHAR in the
SQLBindParameter()call.
Return codes
SQLBindParameter(),
it returns one of the following values: - SQL_SUCCESS
- SQL_SUCCESS_WITH_INFO
- SQL_ERROR
- SQL_INVALID_HANDLE
Diagnostics
The following table lists each SQLSTATE that this function generates, with a description and explanation for each value.
| SQLSTATE | Description | Explanation |
|---|---|---|
| 07006 | Invalid conversion. | The conversion from the data value identified by the fCType argument to the data type that is identified by the fSqlType argument, is not a meaningful conversion. (For example, a conversion from SQL_C_TYPE_DATE to SQL_DOUBLE is not meaningful.) |
| 08S01 | Communication link failure. | The communication link between the application and data source fails before the function completes. |
| 58004 | Unexpected system failure. | An unrecoverable system error occurs. |
| HY001 | Memory allocation failure. | Db2 ODBC is not able to allocate the required memory to support the execution or the completion of the function. |
| HY003 | Program type out of range. | The fCType argument is not a valid data type or SQL_C_DEFAULT. |
| HY004 | Invalid SQL data type. | The specified value for the fSqlType argument is not a valid SQL data type. |
| HY009 | Invalid use of a null pointer. | The argument OutputHandlePtr is a null pointer. |
| HY010 | Function sequence error. | The function is called after SQLExecute() or SQLExecDirect() return
SQL_NEED_DATA, but data is not sent for all data-at-execution parameters. |
| HY013 | Unexpected memory handling error. | Db2 ODBC is not able to access the memory that is required to support execution or completion of the function. |
| HY090 | Invalid string or buffer length. | The specified value for the cbValueMax argument is less than 0. |
| HY093 | Invalid parameter number. | The specified value for the ipar argument is less than 1. |
| HY094 | Invalid scale value. | HY094 is returned when the specified value for the fSqlType is SQL_TYPE_TIMESTAMP and the value for the ibScale argument is less than 0 or greater than 12. |
| HY104 | Invalid precision value. | This SQLSTATE is returned because the specified value for the fSqlType argument is either SQL_DECIMAL or SQL_NUMERIC, and the specified value for the cbColDef argument is less than 1. This SQLSTATE is returned for the following reason:
|
| HY105 | Invalid parameter type. | The fParamType argument
does not specify one of the following values:
|
| HYC00 | Driver not capable. | This SQLSTATE is returned for one or more of the
following reasons:
|
Restrictions
A new value for the pcbValue argument, SQL_DEFAULT_PARAM, was introduced in ODBC 2.0 to indicate that the procedure should use the default value of a parameter, rather than a value sent from the application. Because Db2 stored procedure arguments do not use default values, specification of SQL_DEFAULT_PARAM for the pcbValue argument results in an error when the CALL statement is executed. This error occurs because the SQL_DEFAULT_PARAM value is considered an invalid length.
ODBC 2.0 also introduced the SQL_LEN_DATA_AT_EXEC(length)
macro to be used with the pcbValue argument.
The macro specifies the sum total length of all character C data or
all binary C data that is sent with the subsequent SQLPutData() calls.
Because the Db2 ODBC driver
does not need this information, the macro is not needed. To check
if the driver needs this information, call SQLGetInfo() with
the InfoType argument set to SQL_NEED_LONG_DATA_LEN.
The Db2 ODBC driver returns
'N' to indicate that this information is not needed by SQLPutData().
Example
/* ... */
SQLCHAR stmt[] =
"INSERT INTO PRODUCT VALUES (?, ?, ?, ?, ?)";
SQLINTEGER Prod_Num[NUM_PRODS] = {
100110, 100120, 100210, 100220, 100510, 100520, 200110,
200120, 200210, 200220, 200510, 200610, 990110, 990120,
500110, 500210, 300100
};
SQLCHAR Description[NUM_PRODS][257] = {
"Aquarium-Glass-25 litres", "Aquarium-Glass-50 litres",
"Aquarium-Acrylic-25 litres", "Aquarium-Acrylic-50 litres",
"Aquarium-Stand-Small", "Aquarium-Stand-Large",
"Pump-Basic-25 litre", "Pump-Basic-50 litre",
"Pump-Deluxe-25 litre", "Pump-Deluxe-50 litre",
"Pump-Filter-(for Basic Pump)",
"Pump-Filter-(for Deluxe Pump)",
"Aquarium-Kit-Small", "Aquarium-Kit-Large",
"Gravel-Colored", "Fish-Food-Deluxe-Bulk",
"Plastic-Tubing"
};
SQLDOUBLE UPrice[NUM_PRODS] = {
110.00, 190.00, 100.00, 150.00, 60.00, 90.00, 30.00,
45.00, 55.00, 75.00, 4.75, 5.25, 160.00, 240.00,
2.50, 35.00, 5.50
};
SQLCHAR Units[NUM_PRODS][3] = {
" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", "kg", "kg", "m"
};
SQLCHAR Combo[NUM_PRODS][2] = {
"N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "Y", "Y", "N", "N", "N"
};
SQLUINTEGER pirow = 0;
/* ... */ /* Prepare the statement */
rc = SQLPrepare(hstmt, stmt, SQL_NTS);
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER,
0, 0, Prod_Num, 0, NULL);
rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR,
257, 0, Description, 257, NULL);
rc = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DECIMAL,
10, 2, UPrice, 0, NULL);
rc = SQLBindParameter(hstmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
3, 0, Units, 3, NULL);
rc = SQLBindParameter(hstmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
2, 0, Combo, 2, NULL);
rc = SQLParamOptions(hstmt, NUM_PRODS, &pirow);
rc = SQLExecute(hstmt);
printf("Inserted %ld Rows\n", pirow);
/* ... */