Specification: | DB2 CLI 2.1 | ODBC 1.0 | ISO CLI |
SQLSetStmtAttr() sets options related to a statement.
Syntax
SQLRETURN SQLSetStmtAttr (SQLHSTMT StatementHandle, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER StringLength);
Function arguments
Tabulka 82. SQLSetStmtAttr arguments
Data type | Argument | Use | Description |
---|---|---|---|
SQLHSTMT | StatementHandle | input | Statement handle. |
SQLINTEGER | Attribute | input | Option to set. |
SQLPOINTER | ValuePtr | input | If Attribute is an ODBC-defined attribute and ValuePtr
points to a character string or a binary buffer, this argument should be the
length of *ValuePtr. If Attribute is an ODBC-defined
attribute and ValuePtr is an integer, StringLength is ignored.
If Attribute is a DB2 CLI attribute, the application indicates the nature of the attribute by setting the StringLength argument. StringLength can have the following values:
|
SQLINTEGER | StringLength | input | If ValuePtr points to a character string or a binary buffer, this argument should be the length of ValuePtr. If ValuePtr is a pointer, but does not point to a string or binary buffer, then StringLength should have the value SQL_IS_POINTER. If ValuePtr is not a pointer, then StringLength should have the value SQL_IS_NOT_POINTER. |
Usage
Statement attributes for a statement remain in effect until they are changed by another call to SQLSetStmtAttr() or the statement is dropped by calling SQLFreeHandle(). Calling SQLFreeStmt() with the SQL_CLOSE, SQL_UNBIND, or SQL_RESET_PARAMS options 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 CLI returns SQL_SUCCESS_WITH_INFO and SQLSTATE 01S02 (Option value changed). For example, if Attribute is SQL_ATTR_CONCURRENCY, ValuePtr is SQL_CONCUR_ROWVER, and the data source does not support this, DB2 CLI substitutes SQL_CONCUR_VALUES and returns SQL_SUCCESS_WITH_INFO. To determine the substituted value, an application calls SQLGetStmtAttr(). The format of information set with ValuePtr depends on the specified Attribute.
SQLSetStmtAttr() accepts attribute information in one of two different formats: a null-terminated character string or a 32-bit integer value. The format of each is noted in the attribute's description. This format applies to the information returned for each attribute in SQLGetStmtAttr(). Character strings pointed to by the ValuePtr argument of SQLSetStmtAttr() have a length of StringLength.
DB2 Everyplace uses the dirty bit to track changes made to a record.
The behavior of the dirty bit is affected by the SQL_ATTR_DELETE_MODE,
SQL_ATTR_READ_MODE, and SQL_ATTR_DIRTYBIT_SET_MODE statement
attributes. The following table shows the states of the dirty bit after
certain database operations are performed on a record. The table
assumes that the SQL_ATTR_DIRTYBIT_SET_MODE parameter is set to
SQL_DIRTYBIT_SET_BY_SYSTEM with the dirty bit maintained by the system.
Tabulka 83. DB2 Everyplace dirty bit states
The value of the dirty bit can be obtained by querying the $dirty column of a table. For example, the following statement returns the dirty bit and the NAME column of the PHONEBOOK table:
SELECT $dirty, NAME from PHONEBOOK
The dirty bit can have the following values.
Tabulka 84. DB2 Everyplace dirty bit values
Description |
Dirty bit value
|
---|---|
Record unchanged (CLEAN) | 0 |
Record deleted (DELETE) | 1 |
Record inserted (INSERT) | 2 |
Record updated (UPDATE) | 3 |
Statement attributes
The currently defined attributes are shown below.
Scrollable cursors are not required on the statement handle. If the application calls SQLFetchScroll() on this handle, the only valid value of FetchOrientation() is SQL_FETCH_NEXT. This is the default.
Scrollable cursors are required on the statement handle. When calling SQLFetchScroll(), the application might specify any valid value of FetchOrientation so that the cursor can be positioned in modes other than the sequential mode.
The write activity of other cursors has an undefined impact on the current cursor. This is the default.
The write activity of other cursors has no impact on the current cursor.
This option cannot be specified for an open cursor.
This is the system default. When a delete SQL statement is executed, records are only marked as "delete". The record contents can still be read if the SQL_READ_INCLUDE_MARKED_DELETE is set.
A delete SQL statement physically removes the records meeting the WHERE clause condition, regardless of its dirty bit.
For example, use the following syntax to physically remove some records ignoring the status of the dirty bits:
SQLSetStmtAttr (stmt, SQL_ATTR_DELETE_MODE, SQL_DELETE_PHYSICAL_REMOVE, 0)
Next execute the following SQL statement to delete all records from table T where X is not equal to 0:
DELETE T WHERE X<>0
This is the system default. A record that is inserted, updated, or deleted has a dirty bit that is set to INSERT, UPDATE, or DELETE, respectively. No UPDATE of the $dirty column is allowed when the SQL_DIRTYBIT_SET_BY_SYSTEM is set.
The application is responsible for setting the dirty bit when inserting, updating, or deleting records. The semantics for each operation are:
UPDATE T SET $dirty=0 WHERE $dirty>0
For example, to clean the dirty bit of a record use the following statement:
SQLSetStmtAttr (stmt, SQL_ATTR_DIRTYBIT_SET_MODE, SQL_DIRTYBIT_SET_BY_APPLICATION, 0)
Then execute the following SQL statement:
UPDATE T SET $DIRTY=0 WHERE $DIRTY>0
In general, applications can set SQL_DIRTYBIT_SET_BY_APPLICATION when the dirty bits are not needed for tracking database updates by end-users.
This is the system default. All records with the dirty bit set to "delete" are hidden from SQL.
Once set, the records with the dirty bit set to DELETE are visible from SQL SELECT statement. Applications can distinguish those deleted records from other records by examining the dirty bit for a record.
For example, use the following statement to read all records with the dirty bit set, including those with dirty bits marked as DELETE:
SQLSetStmtAttr (stmt, SQL_ATTR_READ_MODE, SQL_READ_INCLUDE_MARKED_DELETE, 0)
then execute the following SQL statement to retrieve all records:
SELECT * FROM T WHERE $dirty<>0
This option cannot be specified for an open cursor.
Return codes
Diagnostics
Tabulka 85. SQLSetStmtAttr SQLSTATEs
SQLSTATE | Description | Explanation |
---|---|---|
24000 | Invalid cursor state. | A cursor is already opened on the statement handle. |
HY001 | Memory allocation failure. | DB2 CLI is unable to allocate memory required to support execution or completion of the function. |
HY010 | Function sequence error. | The function is called while in a data-at-execute
(SQLPrepare() or SQLExecDirect()) operation.
The function is called while within a BEGIN COMPOUND and END COMPOUND SQL operation. |
HY014 | No more handles. | DB2 CLI is unable to allocate a handle due to internal resources. |
HY090 | Invalid string or buffer length. | The value of one of the name length arguments is less than 0, but not equal SQL_NTS. |
Restrictions
None.
Související odkazy