SQLSetStmtAttr--Set options related to a statement

Purpose

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

Table 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:

  • If ValuePtr is a pointer to a character string, then StringLength is the length of the string or SQL_NTS.
  • If ValuePtr is a pointer to a binary buffer, then the application places the result of the SQL_LEN_BINARY_ATTR(length) macro in StringLength. This places a negative value in StringLength.
  • If ValuePtr is a pointer to a value other than a character string or a binary string, then StringLength should have the value SQL_IS_POINTER.
  • If ValuePtr contains a fixed-length value, then StringLength is either SQL_IS_INTEGER or SQL_IS_UINTEGER.
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.

The dirty bit

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.

Table 83. DB2 Everyplace dirty bit states

Actions on a record Dirty bit state
clean state (0) then INSERT INSERT
clean state (0) then DELETE DELETE
clean state (0) then UPDATE UPDATE
DELETE then INSERT UPDATE
DELETE then DELETE Not applicable
DELETE then UPDATE Not applicable
INSERT then INSERT Not applicable
INSERT then DELETE Physical removal of record
INSERT then UPDATE INSERT
UPDATE then INSERT Not applicable
UPDATE then DELETE DELETE
UPDATE then UPDATE UPDATE

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.

Table 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.

SQL_ATTR_CURSOR_SCROLLABLE (DB2 CLI)
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 (DB2 CLI)
A 32-bit integer value that specifies whether a cursor is sensitive to the write activity of another cursor. The supported values are:

SQL_ATTR_CURSOR_TYPE (DB2 CLI)
A 32-bit integer value that specifies the cursor type. The supported values are:

This option cannot be specified for an open cursor.

SQL_ATTR_ROW_ARRAY_SIZE (DB2 CLI)
A 32-bit integer value that specifies the number of rows in the row set. This is the number of rows returned by each call to SQLFetch() or SQLFetchScroll(). The default value is 1. If the specified row set size exceeds the maximum row set size supported by the data source, DB2 CLI substitutes that value and returns SQLSTATE 01S02 (Option value changed). This option can be specified for an open cursor.

SQL_ATTR_ROW_BIND_TYPE (DB2 CLI)
A 32-bit integer value that sets the binding orientation to be used when SQLFetch() or SQLFetchScroll() is called on the associated statement. Column-wise binding is selected by supplying the defined constant SQL_BIND_BY_COLUMN in ValuePtr. The length specified in ValuePtr 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. Column-wise binding is the default binding orientation for SQLFetchScroll().

SQL_ATTR_ROW_NUMBER (DB2 CLI)
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 CLI returns 0. This attribute can be retrieved by a call to SQLGetStmtAttr(), but not set by a call to SQLSetStmtAttr().

SQL_ATTR_ROW_STATUS_PTR (DB2 CLI)
A 16-bit unsigned integer value that points to an array of UWORD values containing row status values after a call to SQLFetch() or SQLFetchScroll(). The array has as many elements as there are rows in the row set. This statement attribute can be set to a null pointer, in which case DB2 CLI 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 SQLFetch() or SQLFetchScroll() is called.

SQL_ATTR_ROWS_FETCHED_PTR (DB2 CLI)
A 32-bit unsigned integer value that points to a buffer in which to return the number of rows fetched after a call to SQLFetch() or SQLFetchScroll().

SQL_ATTR_DELETE_MODE (DB2 Everyplace)
The supported values are:

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

SQL_ATTR_DIRTYBIT_SET_MODE (DB2 Everyplace)
A 32-bit integer value that specifies the cursor type. The supported values are:

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.

SQL_ATTR_READ_MODE (DB2 Everyplace)
A 32-bit integer value that specifies the cursor type. The supported values are:

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

SQL_ATTR_REORG_MODE (DB2 Everyplace)
A 32-bit integer value that specifies whether automatic database reorganization is performed on user created tables and whether explicit REORG SQL statements are allowed. The supported values are:

This option cannot be specified for an open cursor.

Return codes

Diagnostics

Table 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.

Related reference