SQLSetStmtAttr()
- Set statement attributes
SQLSetStmtAttr()
sets attributes that
are related to a statement. To set an attribute for all statements
that are associated with a specific connection, an application can
call SQLSetConnectAttr()
.
ODBC specifications for SQLSetStmtAttr()
ODBC specification level | In X/Open CLI CAE specification? | In ISO CLI specification? |
---|---|---|
3.0 | Yes | Yes |
Syntax
SQLRETURN SQLSetStmtAttr (SQLHSTMT StatementHandle,
SQLINTEGER Attribute,
SQLPOINTER ValuePtr,
SQLINTEGER StringLength);
Function arguments
The following table lists the data type, use, and description for each argument in this function.
Data type | Argument | Use | Description |
---|---|---|---|
SQLHSTMT | StatementHandle | input | Statement handle. |
SQLINTEGER | Attribute | input | Statement attribute to set. Refer to Table 3 for a complete list of attributes. |
SQLPOINTER | ValuePtr | input | Pointer to the value to be associated with Attribute. Depending on the value of Attribute, ValuePtr will be a 32-bit unsigned integer value or point to a nul-terminated character string. If the Attribute argument is a driver-specific value, the value in ValuePtr might be a signed integer. |
SQLINTEGER | StringLength | input | Information about the *ValuePtr argument.
|
Usage
Statement attributes
for a statement remain in effect until they are changed by another
call to SQLSetStmtAttr()
or until the statement is
dropped by calling SQLFreeHandle()
. Calling SQLFreeStmt()
with
the SQL_CLOSE, SQL_UNBIND or the SQL_RESET_PARAMS attribute does not
reset statement attributes.
Some statement attributes support
substitution of a similar value if the data source does not support
the value specified in *ValuePtr. In such
cases, Db2 ODBC returns SQL_SUCCESS_WITH_INFO
and SQLSTATE 01S02 (attribute value changed). To determine
the substituted value, an application calls SQLGetStmtAttr()
.
The
format of the information set with ValuePtr depends
on the specified Attribute. SQLSetStmtAttr()
accepts
attribute information either in the format of a nul-terminated character
string or a 32-bit integer value. The format of each ValuePtr value
is noted in the attribute descriptions shown in Table 3. This format applies
to the information returned for each attribute in SQLGetStmtAttr()
.
Character strings that the ValuePtr argument
of SQLSetStmtAttr()
point to have a length of StringLength.
Db2 ODBC supports all of the ODBC
2.0 Attribute values that are renamed in
ODBC 3.0. For a summary of the Attribute values
renamed in ODBC 3.0, refer to "Changes to SQLSetStmtAttr()
attributes".
Overriding Db2 CCSIDs from DSNHDECP: Db2 ODBC extensions to SQLSetStmtAttr()
allow
an application to override the Unicode, EBCDIC, or ASCII CCSID
settings of the Db2 subsystem
to which they are currently attached, using the statement attributes
SQL_CCSID_CHAR and SQL_CCSID_GRAPHIC. This extension is intended
for applications that are attempting to send and receive data to and
from Db2 in a CCSID that differs
from the default settings in the Db2 DSNHDECP.
The
CCSID override applies only to input data bound to parameter markers
through SQLBindParameter()
or SQLBindFileToParam()
,
output data that is bound to columns through SQLBindCol()
or SQLBindFileToCol()
, and output data
that is retrieved through SQLGetData()
.
The CCSID override applies on a statement level only. Db2 will continue to use the default CCSID settings in the Db2 DSNHDECP after the statement is dropped or if SQL_CCSID_DEFAULT is specified.
You
can use SQLGetStmtAttr()
to query the settings of
the current statement handle CCSID override.
The following
table lists each Attribute value SQLSetStmtAttr()
can
set. Values shown in bold are default values.
Attribute | ValuePtr contents |
---|---|
SQL_ATTR_BIND_TYPE or SQL_ATTR_ROW_BIND_TYPE | A 32-bit integer value
that sets the binding orientation to be used when SQLExtendedFetch() is
called with this statement handle. Column-wise binding is
selected by supplying the value SQL_BIND_BY_COLUMN for the
argument vParam. Row-wise binding is
selected by supplying a value for vParam specifying
the length (in bytes) of the structure or an instance of a buffer
into which result columns are bound. For row-wise binding, the length (in bytes) specified in vParam must include space for all of the bound columns and any padding of the structure or buffer to ensure that when the address of a bound column is incremented with the specified length, the result points to the beginning of the same column in the next row. (When using the sizeof operator with structures or unions in ANSI C, this behavior is guaranteed.) |
SQL_CCSID_CHAR | A 32-bit integer value
that specifies the CCSID of:
The CCSID applies to data for which the C symbolic data type
is SQL_C_CHAR and the SQL data type is one of the following types:
|
SQL_CCSID_GRAPHIC | A 32-bit integer value
that specifies the CCSID of:
The CCSID applies to data for which the C symbolic data type
is SQL_C_DBCHAR and the SQL data type is one of the following types:
|
SQL_ATTR_CLIENT_TIME_ZONE | A null-terminated character string in the format ±hh:mm, containing the time zone information with values ranging from -12:59 and +14:00. Specifying this attribute overrides the client's OS default time zone. |
SQL_ATTR_CLOSE_BEHAVIOR | A 32-bit integer value
that forces the release of locks upon an underlying CLOSE CURSOR operation.
The possible values are:
SQLFreeStmt() is
called with the fOption argument set to
SQL_CLOSE or SQLCloseCursor() is called. In addition,
the end of the transaction (when a commit or rollback is issued) can
also close the cursor (depending on the WITH HOLD attribute currently
in use). |
SQL_ATTR_CONCURRENCY | A 32-bit integer value
that specifies the cursor concurrency:
The default value for SQL_ATTR_CONCURRENCY is SQL_CONCUR_READ_ONLY for static and forward-only cursors. The default for dynamic cursors is SQL_CONCUR_LOCK. If the SQL_ATTR_CURSOR_TYPE attribute is
changed to a type that does not support the current value of SQL_ATTR_CONCURRENCY,
the value of SQL_ATTR_CONCURRENCY is changed at execution time, and
a warning is issued when If a SELECT FOR UPDATE statement is executed when the value of SQL_ATTR_CONCURRENCY is set to SQL_CONCUR_READ_ONLY, an error is returned. If the value of SQL_ATTR_CONCURRENCY is changed
to a value that is supported for some value of SQL_ATTR_CURSOR_TYPE,
but not for the current value of SQL_ATTR_CURSOR_TYPE, the value of
SQL_ATTR_CURSOR_TYPE is changed at execution time, and a warning is
issued when If the specified concurrency is not supported by the
data source, Db2 ODBC substitutes
a different concurrency and returns a warning. The order of substitution
depends on the cursor type:
Unsupported attribute values: ODBC architecture
defines the following values, which are not supported by Db2 ODBC:
|
SQL_ATTR_CURSOR_HOLD1 | A 32-bit integer which
specifies whether the cursor associated with this statement handle
is preserved in the same position as before the COMMIT operation,
and whether the application can fetch without executing the statement
again.
The default value when a statement handle is first allocated is SQL_CURSOR_HOLD_ON. This attribute cannot be specified while there is an open cursor on this statement handle. |
SQL_ATTR_CURSOR_SCROLLABLE | A 32-bit integer that specifies the level of support
that the application requires. Setting this attribute affects subsequent
calls to SQLExecDirect() and SQLExecute() .
The supported values are: |
SQL_ATTR_CURSOR_SENSITIVITY | A 32-bit integer that specifies whether changes
that are made by other cursors are visible to the cursors on the statement
handle. Setting this attribute affects subsequent calls to SQLExecDirect() and SQLExecute() .
The supported values are:
|
SQL_ATTR_CURSOR_TYPE | A 32-bit integer value
that specifies the cursor type. The supported values are:
If the specified cursor type is not supported by the data source, Db2 ODBC substitutes a different cursor type and returns a warning. For a dynamic cursor, Db2 ODBC substitutes a different cursor type, in the following order: a static cursor or a forward-only cursor. Unsupported attribute values: ODBC
architecture defines the SQL_CURSOR_KEYSET_DRIVEN value, which is
not supported by Db2 ODBC. If
this value is specified, Db2 ODBC
sets the statement attribute to SQL_CURSOR_STATIC or SQL_CURSOR_FORWARD_ONLY,
and returns SQLSTATE 01S02 (Option value changed).
In this case the application needs to call |
SQL_ATTR_MAX_LENGTH | A 32-bit integer value
corresponding to the maximum amount of data that can be retrieved
from a single character or binary column. If data is truncated because
the value specified for SQL_ATTR_MAX_LENGTH is less than the amount
of data available, an SQLGetData() call or fetch
returns SQL_SUCCESS instead of returning SQL_SUCCESS_WITH_INFO and
SQLSTATE 01004 (data truncated). The default value for vParam is 0;
0 means that Db2 ODBC attempts
to return all available data for character or binary type data. |
SQL_ATTR_MAX_ROWS | A 32-bit integer value corresponding to the maximum number of rows to return to the application from a query. The default value for vParam is 0; 0 means all rows are returned. |
SQL_ATTR_NODESCRIBE | A 32-bit integer
which specifies whether Db2 ODBC
should automatically describe the column attributes of the result
set or wait to be informed by the application using
SQLSetColAttributes() .
This attribute cannot be specified while there is an open cursor on this statement handle. This attribute is used
in conjunction with the function IBM specific: This attribute is an IBM-defined extension. |
SQL_ATTR_NOSCAN | A 32-bit integer value
that specifies whether Db2 ODBC
will scan SQL strings for escape clauses. The two permitted values
are:
|
SQL_ATTR_PARAMOPT_ATOMIC | A 32-bit integer value that determines whether
the application uses atomic or non-atomic SQL for the underlying processing
of multi-row insert operations. The attribute value takes effect after SQLSetStmtAttr() is
used to specify multiple values for parameter markers for an SQL INSERT
statement. Possible values are:
|
SQL_ATTR_PARAMSET_SIZE | A 32-bit unsigned integer value that specifies the number of values for each parameter. If SQL_ATTR_PARAMSET_SIZE is greater than 1, the rgbValue argument in SQLBindParameter() points to an array of parameter values and pcbValue argument points to an array of lengths. The cardinality of each array is equal to the value of this field. |
SQL_ATTR_PARAMS_PROCESSED_PTR | A 32-bit unsigned integer * field that points to a buffer in which to return the current row number. As each row of parameters is processed, this is set to the number of that row. No row number is returned if this is a null pointer. If the call to SQLExecDirect() or SQLExecute() that fills in the buffer pointed to by this attribute does not return SQL_SUCCESS or SQL_SUCCESS_WITH_INFO, the contents of the buffer are undefined. |
SQL_ATTR_RETRIEVE_DATA | A 32-bit integer value
that indicates whether Db2 ODBC
should retrieve data from the database when SQLFetchScroll() or SQLExtendedFetch() is
called. The possible values are:
This attribute cannot be set if the cursor is open. |
SQL_ATTR_ROW_ARRAY_SIZE | A 32-bit integer value
that specifies the number of rows in the row set. This is the number
of rows that are returned by each call toSQLFetchScroll() . The
default value is 1. If the specified rowset size exceeds the maximum rowset size that is supported by the data source, Db2 ODBC substitutes the maximum supported value and returns SQLSTATE 01S02 (Option value changed). |
SQL_ATTR_ROW_NUMBER | A 32-bit integer value that is the number of the
current row in the entire result set. If the number of the current
row cannot be determined, or there is no current row, Db2 ODBC returns 0. This attribute can be retrieved
by a call to SQLGetStmtAttr() , but not set by a call
to SQLSetStmtAttr() . |
SQL_ATTR_ROW_OPERATION_POINTER | A 16-bit unsigned integer * value that points to
an array of UDWORD values that are used to ignore a row when SQLSetPos() is
used to perform a bulk operation. Each value is set to SQL_ROW_PROCEED
(for the row to be included in the bulk operation) or SQL_ROW_IGNORE
(for the row to be excluded from the bulk operation). During calls
to SQLBulkOperations() , rows cannot
be ignored by using this array.If
SQL_ATTR_ROW_OPERATION_POINTER is set to a null pointer, Db2 ODBC does not return row status values. This
attribute can be set at any time, but the new value is not used until
the next time |
SQL_ATTR_ROW_STATUS_PTR | A 16-bit unsigned integer * value that points to
an array of UWORD values that contain row status values after a call
to SQLFetch() or SQLFetchScroll() .
The array has as many elements as there are rows in the rowset.If
SQL_ATTR_ROW_STATUS_PTR is set to a null pointer, Db2 ODBC does not return row status values.
This attribute can be set at any time, but the new value is not used
until the next time |
SQL_ATTR_ROWS_FETCHED_PTR | A 32-bit unsigned integer * value that points to a buffer that contains the
number of rows that were fetched after a call to SQLFetch() or
SQLFetchScroll() . The array has as many elements as there are rows in the
rowset.Db2 ODBC maps SQL_ATTR_ROWS_FETCHED_PTR to the
RowCountPtr array in a call to
|
SQL_ATTR_ROWSET_SIZE | A 32-bit integer value
that specifies the number of rows in the row set. A row set is the
array of rows that is returned by each call to SQLExtendedFetch() .
The default value is 1, which is equivalent to making a single SQLFetch() call.
This attribute can be specified even when the cursor is open and becomes
effective on the next SQLExtendedFetch() call.Recommendation: Use |
SQL_ATTR_STMTTXN_ISOLATION or SQL_ATTR_TXN_ISOLATION2 | A 32-bit integer value
that sets the transaction isolation level for the current statement
handle. This overrides the default value set at the connection level.
For the permitted values, refer to the function SQLSetConnectOption() This attribute cannot be set if there is an open cursor on this statement handle (SQLSTATE 24000). IBM specific: The value SQL_ATTR_STMTTXN_ISOLATION is synonymous with SQL_ATTR_TXN_ISOLATION. SQL_ATTR_STMTTXN_ISOLATION is an IBM extension to allow setting this attribute at the statement level. For more information about setting this attribute, refer to Isolation levels for maximum concurrency and data consistency. |
SQL_ATTR_USE_BOOKMARKS | A 32-bit integer value that specifies whether an
application uses bookmarks with a cursor. The only supported attribute
value is SQL_UB_OFF, which indicates that an application does not
use bookmarks with a cursor. Unsupported attribute value: ODBC architecture defines the SQL_UB_VARIABLE value, which is not supported by Db2 ODBC. SQL_UB_VARIABLE indicates that an application uses bookmarks with a cursor, and that the ODBC driver provides variable-length bookmarks, if they are supported. |
Notes:
|
Return codes
SQLSetStmtAttr()
,
it returns one of the following values: - SQL_SUCCESS
- SQL_SUCCESS_WITH_INFO
- SQL_INVALID_HANDLE
- SQL_ERROR
Diagnostics
The following table lists each SQLSTATE that this function generates, with a description and explanation for each value.
SQLSTATE | Description | Explanation |
---|---|---|
01000 | Warning. | Informational message. (SQLSetStmtAttr() returns
SQL_SUCCESS_WITH_INFO for this SQLSTATE.) |
01S02 | Option value changed. | Db2 did not support the value specified in *ValuePtr, or the value specified in *ValuePtr is invalid due to SQL constraints or requirements. Therefore, Db2 ODBC substituted a similar value. (SQLSetStmtAttr() returns SQL_SUCCESS_WITH_INFO for this SQLSTATE.) |
08S01 | Unable to connect to data source. | The communication link between the application and the data source failed before the function completed. |
24000 | Invalid cursor state. | The Attribute is SQL_ATTR_CONCURRENCY and the cursor is open. |
HY000 | General error. | An error occurred for which no specific
SQLSTATE exists. The error message returned by SQLGetDiagRec() in
the *MessageText buffer describes the error
and its cause. |
HY001 | Memory allocation failure. | Db2 ODBC is not able to allocate memory for the specified handle. |
HY009 | Invalid use of a null pointer. | A null pointer is passed for ValuePtr and the value in *ValuePtr is a string value. |
HY010 | Function sequence error. | SQLExecute() or SQLExecDirect() is
called with the statement handle, and returns SQL_NEED_DATA. This
function is called before data is sent for all data-at-execution parameters
or columns. Invoke SQLCancel() to cancel the data-at-execution
condition. |
HY011 | Operation invalid at this time. | The Attribute is SQL_ATTR_CONCURRENCY and the statement is prepared. |
HY024 | Invalid attribute value. | Given the specified Attribute value, an invalid value is specified in *ValuePtr. |
HY090 | Invalid string or buffer length. | The StringLength argument is less than 0, but is not SQL_NTS. |
HY092 | Option type out of range. | The value specified for the argument Attribute is not valid for this version of Db2 ODBC. |
HYC00 | Driver not capable. | The value specified for the argument Attribute is a valid connection or statement attribute for the version of the Db2 ODBC driver, but is not supported by the data source. |
Example
SQLSetStmtAttr()
to set statement attributes:
rc = SQLSetStmtAttr( hstmt,
SQL_ATTR_CURSOR_HOLD,
( void * ) SQL_CURSOR_HOLD_OFF,
0 ) ;
CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;