SQLBindParameter
SQLBindParameter#
SQLBindParameter binds the parameter marker of a SQL statement with an application variables. The data is transmitted from an application to the database when SQLExecute() is called.
Syntax#
SQLRETURN SQLBindParameter (
SQLHSTMT stmt,
SQLSMALLINT par,
SQLSMALLINT pType,
SQLSMALLINT cType,
SQLSMALLINT sqlType,
SQLULEN columnSize,
SQLSMALLINT scale,
SQLPOINTER value,
SQLLEN valueMax,
SQLLEN * valueLength );
Arguments#
Data Type | Argument | In/Output | Description |
---|---|---|---|
SQLHSTMT | stmt | Input | Statement handle |
SQLSMALLINT | par | Input | Parameter order. Starting with 1. |
SQLSMALLINT | pType | Input | Parameter type. All parameters in a SQL statement must be input variables (SQL_PARAM_INPUT). When executing a stored procedure, arguments can be are input, output, or input/output type variables. (SQL_PARAM_INPUT, SQL_PARAM_OUTPUT, SQL_PARAM_INPUT_OUTPUT) |
SQLSMALLINT | cType | Input | C data type of the parameter (SQL_C_CHAR, SQL_C_SBIGINT, etc) See: Appendix of this document |
SQLSMALLINT | sqlType | Input | SQL data type of the parameter (SQL_CHAR, SQL_VARCHAR, etc) See: Appendix of this document |
SQLULEN | columnSize | Input | An argument that indicates the precision of a parameter marker. Based on SQL type, it can be used as follows: * SQL_CHAR, SQL_VARCHAR: Indicates the max allowed length of a parameter marker. (If columnSize is 0, the default columnSize is used. For SQL_CHAR and SQL_VARCHAR, their columnSize is 32,000.) * SQL_DECIMAL, SQL_NUMERIC: Indicates the decimal significant digits of a parameter marker. (If columnSize is 0, the default columnSize is used. For both SQL_DECIMAL and SQL_NUMERIC, the columnSize is 38, which is the max number of decimal significant digits.) * SQL_BINARY, SQL_BYTES, SQL_NIBBLE, SQL_VARBIT: Indicates the max allowed length of a parameter marker .(If columnSize is 0, the default columnSize is used. The columnSize for each type is as follows: For SQL_BINARY, SQL_BYTE and SQL_VARBIT, their columnSize is 32000. For SQL_NIBBLE, its columnSize is 254.) * For other types, the user-defined columnSize argument is ignored and the following fixed value is used. SQL_SMALLINT 5 SQL_INTEGER 10 SQL_BIGINT 19 SQL_REAL 7 SQL_FLOAT 38 SQL_DOUBLE 15 SQL_TYPE_DATE 30 SQL_TYPE_TIME 30 SQL_TYPE_TIMESTAMP 30 SQL_INTERVAL 10 SQL_GEOMETRY 3200 |
SQLSMALLINT | scale | Input | Decimal number according to *Value or parameter marker If the SQL data type is SQL_NUMERIC, the number of digits after the decimal point |
SQLPOINTER | value | Input (Suspended) | The pointer of the actual data about the parameter when SQLExecute() or SQLExecDirect() is called. |
SQLLEN | valueMax | Input/Out | Maximum length of the *Value buffer for the character or binary C data |
SQLLEN * | valueLength | Input (Suspended) | Pointer of the input/output data length when SQLExecute() or SQLExecDirect() is called |
Return Values#
SQL_SUCCESS
SQL_SUCCESS_WITH_INFO
SQL_INVALID_HANDLE
SQL_ERROR
Description#
Binding Arrays
The array binding method reduces the network round-trip count and improves the speed by sending the parameter using array types.
The following figure briefly shows how works array binding. Larger amount of data can be sent in a shorter time due to reduced network paging count.
[Figure 2‑1] Array Binding
There are two array binding types:
Column-wise Parameter Binding#
When using column-wise binding, an application binds one or two, or in some cases three, arrays to each column for which data is to be returned. To use a column-wise binding, do the following:
Set SQL_ATTR_PARAM_BIND_TYPE in Arguments Attribute of an application function SQLSetStmtAttr().
Set SQL_PARAM_BIND_BY_COLUMN in param.
- Allocate the parameter buffer array.
- Allocate the indicator buffer array.
-
Call SQLBindParameter() with arguments.
-
cType is C data type of the single element in the parameter buffer array.
- sqlType is the SQL data type of the parameter.
- Value is the address of the parameter buffer array.
- valueMax is the size of the single element in the parameter buffer array.
- valueLength is the address of the length/indicator array.
The following figure shows how the column-wise binding operates for each column.
[Figure 2‑2] Column-wise Binding
Example#
#define DESC_LEN 51
#define ARRAY_SIZE 10
SQLCHAR * Statement = "INSERT INTO Parts (PartID, Description, Price) "
"VALUES (?, ?, ?)";
SQLUINTEGER PartIDArray[ARRAY_SIZE];
SQLCHAR DescArray[ARRAY_SIZE][DESC_LEN];
SQLREAL PriceArray[ARRAY_SIZE];
SQLINTEGER PartIDIndArray[ARRAY_SIZE], DescLenOrIndArray[ARRAY_SIZE],
PriceIndArray[ARRAY_SIZE];
SQLUSMALLINT i, ParamStatusArray[ARRAY_SIZE];
SQLUINTEGER ParamsProcessed;
// Set the SQL_ATTR_PARAM_BIND_TYPE statement attribute to use
// column-wise binding.
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_BIND_TYPE, SQL_PARAM_BIND_BY_COLUMN, 0);
// Specify the number of elements in each parameter array.
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE, ARRAY_SIZE, 0);
// Specify an array in which to return the status of each set of
// parameters.
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_STATUS_PTR, ParamStatusArray, 0);
// Specify an SQLUINTEGER value in which to return the number of sets of
// parameters processed.
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMS_PROCESSED_PTR, &ParamsProcessed, 0);
// Bind the parameters in column-wise fashion.
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 5, 0,
PartIDArray, 0, PartIDIndArray);
SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, DESC_LEN - 1, 0,
DescArray, DESC_LEN, DescLenOrIndArray);
SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_REAL, 7, 0,
PriceArray, 0, PriceIndArray);
Row-wise Parameter Binding#
When using row style bindings, the application defines a structure (array) that contains a parameter and a length / indicator buffer for each parameter to be bound.
An application performs the next procedures to use the row-wise binding.
Define the array to include the single set of the parameters (including parameters and the length/indicator buffers).
Set SQL_ATTR_PARAM_BIND_TYPE in argument attributes of function SQLSetStmtAttr(), and set the size of the array including program variables in the argument parameter, and binds the address of each element to the first element of the array.
Call SQLBindParameter() with following arguments.
- cType is the component type of the parameter buffer.
- sqlType is the SQL data type of the parameter.
- Value is the address of the parameter buffer component in the first array element.
- valueMax is the size of the parameter buffer component.
- valueLength is the address of the length/indicator to be bound
The following figure shows how row-wise binding operates.
[Figure 2‑3] Row-wise Binding
Example#
#define DESC_LEN 51
#define ARRAY_SIZE 10
typedef tagPartStruct {
SQLREAL Price;
SQLUINTEGER PartID;
SQLCHAR Desc[DESC_LEN];
SQLINTEGER PriceInd;
SQLINTEGER PartIDInd;
SQLINTEGER DescLenOrInd;
} PartStruct;
PartStruct PartArray[ARRAY_SIZE];
SQLCHAR * Statement = "INSERT INTO Parts (PartID, Description,
Price) "
"VALUES (?, ?, ?)";
SQLUSMALLINT i, ParamStatusArray[ARRAY_SIZE];
SQLUINTEGER ParamsProcessed;
// Set the SQL_ATTR_PARAM_BIND_TYPE statement attribute to use
// column-wise binding.
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_BIND_TYPE, sizeof(PartStruct), 0);
// Specify the number of elements in each parameter array.
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE, ARRAY_SIZE, 0);
// Specify an array in which to return the status of each set of
// parameters.
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_STATUS_PTR, ParamStatusArray, 0);
// Specify an SQLUINTEGER value in which to return the number of sets of
// parameters processed.
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMS_PROCESSED_PTR, &ParamsProcessed, 0);
// Bind the parameters in row-wise fashion.
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 5, 0,
&PartArray[0].PartID, 0, &PartArray[0].PartIDInd);
SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, DESC_LEN - 1, 0,
PartArray[0].Desc, DESC_LEN, &PartArray[0].DescLenOrInd);
SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_REAL, 7, 0,
&PartArray[0].Price, 0, &PartArray[0].PriceInd);
Constraints#
For SQL_BINARY, SQL_BYTES, SQL_NIBBLE and SQL_VARBIT types, the buffer size and column size must be specified.
For SQL_CHAR and SQL_VARCHAR types, the default precision is the max size that a column can have. For SQL_NUMERIC and SQL_NUMBER types, the precision is 38.
Cautions#
If the value argument is set to a pointer to an empty string ('') and the cType argument is set to SQL_C_CHAR, the sqlType argument is set to 0 if the native type (SQL_DOUBLE, SQL_REAL, SQL_BIGINT, SQL_INTEGER, SQL_SMALLINT) is set. If it is set as non-native type (SQL_NUMERIC, SQL_DECIMAL, SQL_FLOAT), NULL is entered.
Diagnosis#
SQLSTATE | Description | Comments |
---|---|---|
07006 | Violation of the limited data type attributes | A cType data type cannot be converted into a sqlType data type |
07009 | Invalid numberInvalid number | Indicated par value is smaller than 1 |
HY000 | General error | |
HY001 | Memory allocation error | Failed to allocate the memory for the explicit handle |
HY003 | An application buffer type is not valid | A cType value is invalided C data type. |
HY009 | Invalid pointer used(null pointer) | valueLength is a NULL pointer and pType is not SQL_PARAM_OUTPUT. |
HY090 | Invalid buffer length | valueMax value is smaller than 0 or higher than 64K |
HY105 | Invalid parameter type | pType is invalided value (in, out, inout) |
Related Functions#
SQLExecDirect
SQLExecute
SQLFreeStmt
Example#
< Refer to: $ALTIBASE_HOME/sample/SQLCLI/demo_ex2.cpp >
sprintf(query,"INSERT INTO DEMO_EX2 VALUES( ?, ?, ?, ?, ?, ? )");
/* prepares an SQL string for execution */
if (SQLPrepare(stmt, (SQLCHAR *)query, SQL_NTS) != SQL_SUCCESS)
{
execute_err(dbc, stmt, query);
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
/* binds a buffer to a parameter marker in an SQL statement */
if (SQLBindParameter(stmt,
1, /* Parameter number, starting at 1 */
SQL_PARAM_INPUT, /* in, out, inout */
SQL_C_CHAR, /* C data type of the parameter */
SQL_CHAR, /* SQL data type of the parameter : char(8)*/
8, /* size of the column or expression, precision */
0, /* The decimal digits, scale */
id, /* A pointer to a buffer for the parameter's data */
sizeof(id), /* Length of the ParameterValuePtr buffer in bytes */
&id_ind /* indicator */
) != SQL_SUCCESS)
{
execute_err(dbc, stmt, query);
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
if (SQLBindParameter(stmt, 2, SQL_PARAM_INPUT,
SQL_C_CHAR, SQL_VARCHAR,
20, /* varchar(20) */
0,
name, sizeof(name), &name_ind) != SQL_SUCCESS)
{
execute_err(dbc, stmt, query);
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
if (SQLBindParameter(stmt, 3, SQL_PARAM_INPUT,
SQL_C_SLONG, SQL_INTEGER,
0, 0, &age,
0,/* For all fixed size C data type, this argument is ignored */
NULL) != SQL_SUCCESS)
{
execute_err(dbc, stmt, query);
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
if (SQLBindParameter(stmt, 4, SQL_PARAM_INPUT,
SQL_C_TYPE_TIMESTAMP, SQL_DATE,
0, 0, &birth, 0, NULL) != SQL_SUCCESS)
{
execute_err(dbc, stmt, query);
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
if (SQLBindParameter(stmt, 5, SQL_PARAM_INPUT,
SQL_C_SSHORT, SQL_SMALLINT,
0, 0, &sex, 0, NULL) != SQL_SUCCESS)
{
execute_err(dbc, stmt, query);
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
if (SQLBindParameter(stmt, 6, SQL_PARAM_INPUT,
SQL_C_DOUBLE, SQL_NUMERIC,
10, 3, &etc, 0, &etc_ind) != SQL_SUCCESS)
{
execute_err(dbc, stmt, query);
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
/* executes a prepared statement */
sprintf(id, "10000000");
sprintf(name, "name1");
age = 28;
birth.year=1980;birth.month=10;birth.day=10;
birth.hour=8;birth.minute=50;birth.second=10;
birth.fraction=0;
sex = 1;
etc = 10.2;
id_ind = SQL_NTS; /* id => null terminated string */
name_ind = 5; /* name => length=5 */
etc_ind = 0;
if (SQLExecute(stmt) != SQL_SUCCESS)
{
execute_err(dbc, stmt, query);
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}