Specification: | DB2 CLI 5.0 | ODBC 3.0 | ISO CLI |
SQLGetDiagField() returns the current value of a field of a diagnostic data structure, associated with a specific handle, that contains error, warning, and status information.
Syntax
SQLRETURN SQLGetDiagField (SQLSMALLINT HandleType, SQLHANDLE Handle, SQLSMALLINT RecNumber, SQLSMALLINT DiagIdentifier, SQLPOINTER DiagInfoPtr, SQLSMALLINT BufferLength, SQLSMALLINT *StringLengthPtr);
Function Arguments
Table 102. SQLGetDiagField Arguments
Data Type | Argument | Use | Description |
---|---|---|---|
SQLSMALLINT | HandleType | input | A handle type identifier that describes the type of handle for which
diagnostics are desired. Must be one of the following:
|
SQLHANDLE | Handle | input | A handle for the diagnostic data structure, of the type indicated by HandleType. |
SQLSMALLINT | RecNumber | input | Indicates the status record from which the application seeks information. Status records are numbered from 1. If the DiagIdentifier argument indicates any field of the diagnostics header record, RecNumber must be 0. If not, it should be greater than 0. |
SQLSMALLINT | DiagIdentifier | input | Indicates the field of the diagnostic data structure whose value is to be returned. For more information, see DiagIdentifier Argument. |
SQLPOINTER | DiagInfoPtr | output | Pointer to a buffer in which to return the diagnostic information. The data type depends on the value of DiagIdentifier. |
SQLINTEGER | BufferLength | input | If DiagInfoPtr points to a character string, this argument should be the length of *ValuePtr. If ValuePtr is a pointer, but not to a string, then BufferLength should have the value SQL_IS_POINTER. If ValuePtr is not a pointer, then BufferLength should have the value SQL_IS_NOT_POINTER. If the value in *DiagInfoPtr is a unicode string the BufferLength argument must be an even number. |
SQLSMALLINT | *StringLengthPtr | output | Pointer to a buffer in which to return the total number of bytes (excluding the number of bytes required for the null termination character) available to return in *DiagInfoPtr, for character data. If the number of bytes available to return is greater than BufferLength, then the text in *DiagInfoPtr is truncated to BufferLength minus the length of a null termination character. This argument is ignored for non-character data. |
Usage
An application typically calls SQLGetDiagField() to accomplish one of three goals:
Any DB2 CLI function can post zero or more errors each time it is called, so an application can call SQLGetDiagField() after any function call. SQLGetDiagField() retrieves only the diagnostic information most recently associated with the diagnostic data structure specified in the Handle argument. If the application calls another function, any diagnostic information from a previous call with the same handle is lost.
An application can scan all diagnostic records by incrementing RecNumber, as long as SQLGetDiagField() returns SQL_SUCCESS. The number of status records is indicated in the SQL_DIAG_NUMBER header field. Calls to SQLGetDiagField() are non-destructive as far as the header and status records are concerned. The application can call SQLGetDiagField() again at a later time to retrieve a field from a record, as long as another function other than SQLGetDiagField(), SQLGetDiagRec(), or SQLError() has not been called in the interim, which would post records on the same handle.
An application can call SQLGetDiagField() to return any diagnostic field at any time, with the exception of SQL_DIAG_ROW_COUNT, which will return SQL_ERROR if Handle was not a statement handle on which an SQL statement had been executed. If any other diagnostic field is undefined, the call to SQLGetDiagField() will return SQL_SUCCESS (provided no other error is encountered), and an undefined value is returned for the field.
HandleType Argument
Each handle type can have diagnostic information associated with it. The HandleType argument denotes the handle type of Handle.
Some header and record fields cannot be returned for all types of handles: environment, connection, statement, and descriptor. Those handles for which a field is not applicable are indicated in the Header Field and Record Fields sections below.
No DB2 CLI specific header diagnostic field should be associated with an environment handle.
This argument indicates the identifier of the field desired from the diagnostic data structure. If RecNumber is greater than or equal to 1, the data in the field describes the diagnostic information returned by a function. If RecNumber is 0, the field is in the header of the diagnostic data structure, so contains data pertaining to the function call that returned the diagnostic information, not the specific information.
The following header fields can be included in the DiagIdentifier
argument. The only diagnostic header fields that are defined for a
descriptor field are SQL_DIAG_NUMBER and SQL_DIAG_RETURNCODE.
Table 103. Header Fields for DiagIdentifier Arguments
SQL_DIAG_CURSOR_ROW_COUNT (return type SQLINTEGER) | |
|
This field contains the count of rows in the cursor. Its semantics depend upon the SQLGetInfo() information types:
which indicate which row counts are available for each cursor type (in the SQL_CA2_CRC_EXACT and SQL_CA2_CRC_APPROXIMATE bits). The contents of this field are defined only for statement handles and only after SQLExecute(), SQLExecDirect(), or SQLMoreResults() has been called. Calling SQLGetDiagField() with a DiagIdentifier of SQL_DIAG_CURSOR_ROW_COUNT on other than a statement handle will return SQL_ERROR.
|
SQL_DIAG_DYNAMIC_FUNCTION (return type CHAR *) | |
|
This is a string that describes the SQL statement that the underlying function executed (see Dynamic Function Fields for the values that DB2 CLI supports). The contents of this field are defined only for statement handles, and only after a call to SQLExecute() or SQLExecDirect(). The value of this field is undefined before a call to SQLExecute() or SQLExecDirect().
|
SQL_DIAG_DYNAMIC_FUNCTION_CODE (return type SQLINTEGER) | |
|
This is a numeric code that describes the SQL statement that was executed by the underlying function (see Dynamic Function Fields for the values that DB2 CLI supports). The contents of this field are defined only for statement handles, and only after a call to SQLExecute() or SQLExecDirect(). The value of this field is undefined before a call to SQLExecute(), SQLExecDirect(), or SQLMoreResults(). Calling SQLGetDiagField() with a DiagIdentifier of SQL_DIAG_DYNAMIC_FUNCTION_CODE on other than a statement handle will return SQL_ERROR. The value of this field is undefined before a call to SQLExecute() or SQLExecDirect().
|
SQL_DIAG_NUMBER (return type SQLINTEGER) | |
|
The number of status records that are available for the specified handle.
|
SQL_DIAG_RETURNCODE (return type RETCODE) | |
|
Return code returned by the last executed function associated with the specified handle. See Function Return Codes for a list of return codes. If no function has yet been called on the Handle, SQL_SUCCESS will be returned for SQL_DIAG_RETURNCODE.
|
SQL_DIAG_ROW_COUNT (return type SQLINTEGER) | |
|
The number of rows affected by an insert, delete, or update performed by
SQLExecute(), SQLExecDirect(), or SQLSetPos(). It is defined after a cursor
specification has been executed. The contents of this field are defined
only for statement handles. The data in this field is returned in the
RowCountPtr argument of SQLRowCount().
The data in this field is reset after every function call, whereas the row
count returned by SQLRowCount() remains the
same until the statement is set back to the prepared or allocated
state.
|
The following record fields can be included in the DiagIdentifier
argument:
Table 104. Record Fields for DiagIdentifier Arguments
SQL_DIAG_CLASS_ORIGIN (return type CHAR *) | |
|
A string that indicates the document that defines the class and subclass portion of the SQLSTATE value in this record. DB2 CLI always returns an empty string for SQL_DIAG_CLASS_ORIGIN.
|
SQL_DIAG_COLUMN_NUMBER (return type SQLINTEGER) | |
|
If the SQL_DIAG_ROW_NUMBER field is a valid row number in a rowset or set of parameters, then this field contains the value that represents the column number in the result set. Result set column numbers always start at 1; if this status record pertains to a bookmark column, then the field can be zero. It has the value SQL_NO_COLUMN_NUMBER if the status record is not associated with a column number. If DB2 CLI cannot determine the column number that this record is associated with, this field has the value SQL_COLUMN_NUMBER_UNKNOWN. The contents of this field are defined only for statement handles.
|
SQL_DIAG_CONNECTION_NAME (return type CHAR *) | |
|
A string that indicates the name of the connection that the diagnostic record relates to. DB2 CLI always returns an empty string for SQL_DIAG_CONNECTION_NAME
|
SQL_DIAG_MESSAGE_TEXT (return type CHAR *) | |
|
An informational message on the error or warning.
|
SQL_DIAG_NATIVE (return type SQLINTEGER) | |
|
A driver/data-source-specific native error code. If there is no native error code, the driver returns 0.
|
SQL_DIAG_ROW_NUMBER (return type SQLINTEGER) | |
|
This field contains the row number in the rowset, or the parameter number in the set of parameters, with which the status record is associated. This field has the value SQL_NO_ROW_NUMBER if this status record is not associated with a row number. If DB2 CLI cannot determine the row number that this record is associated with, this field has the value SQL_ROW_NUMBER_UNKNOWN. The contents of this field are defined only for statement handles.
|
SQL_DIAG_SERVER_NAME (return type CHAR *) | |
|
A string that indicates the server name that the diagnostic record relates to. It is the same as the value returned for a call to SQLGetInfo() with the SQL_DATA_SOURCE_NAME InfoType. For diagnostic data structures associated with the environment handle and for diagnostics that do not relate to any server, this field is a zero-length string.
|
SQL_DIAG_SQLSTATE (return type CHAR *) | |
|
A five-character SQLSTATE diagnostic code.
|
SQL_DIAG_SUBCLASS_ORIGIN (return type CHAR *) | |
|
A string with the same format and valid values as SQL_DIAG_CLASS_ORIGIN, that identifies the defining portion of the subclass portion of the SQLSTATE code. DB2 CLI always returns an empty string for SQL_DIAG_SUBCLASS_ORIGIN.
|
Values of the Dynamic Function Fields
The table below describes the values of SQL_DIAG_DYNAMIC_FUNCTION and
SQL_DIAG_DYNAMIC_FUNCTION_CODE that apply to each type of SQL statement
executed by a call to SQLExecute() or SQLExecDirect(). This is the list that DB2 CLI
uses. ODBC also specifies other values.
Table 105. Values of Dynamic Function Fields
SQL Statement Executed | Value of SQL_DIAG_ DYNAMIC_FUNCTION | Value of SQL_DIAG_DYNAMIC_ FUNCTION_CODE |
---|---|---|
alter-table-statement | "ALTER TABLE" | SQL_DIAG_ALTER_TABLE |
create-index-statement | "CREATE INDEX" | SQL_DIAG_CREATE_INDEX |
create-table-statement | "CREATE TABLE" | SQL_DIAG_CREATE_TABLE |
create-view-statement | "CREATE VIEW" | SQL_DIAG_CREATE_VIEW |
cursor-specification | "SELECT CURSOR" | SQL_DIAG_SELECT_CURSOR |
delete-statement-positioned | "DYNAMIC DELETE CURSOR" |
SQL_DIAG_DYNAMIC_DELETE_ CURSOR |
delete-statement-searched | "DELETE WHERE" | SQL_DIAG_DELETE_WHERE |
drop-index-statement | "DROP INDEX" | SQL_DIAG_DROP_INDEX |
drop-table-statement | "DROP TABLE" | SQL_DIAG_DROP_TABLE |
drop-view-statement | "DROP VIEW" | SQL_DIAG_DROP_VIEW |
grant-statement | "GRANT" | SQL_DIAG_GRANT |
insert-statement | "INSERT" | SQL_DIAG_INSERT |
ODBC-procedure-extension | "CALL" | SQL_DIAG_PROCEDURE_CALL |
revoke-statement | "REVOKE" | SQL_DIAG_REVOKE |
update-statement-positioned | "DYNAMIC UPDATE CURSOR" |
SQL_DIAG_DYNAMIC_UPDATE_ CURSOR |
update-statement-searched | "UPDATE WHERE" | SQL_DIAG_UPDATE_WHERE |
Unknown | empty string | SQL_DIAG_UNKNOWN_STATEMENT |
Status records are placed in a sequence based upon row number and the type of the diagnostic.
If there are two or more status records, the sequence of the records is determined first by row number. The following rules apply to determining the sequence of errors by row:
Within each row, or for all those records that do not correspond to a row or for which the row number is unknown, the first record listed is determined using a set of sorting rules. After the first record, the order of the other records affecting a row is undefined. An application cannot assume that errors precede warnings after the first record. Applications should scan the entire diagnostic data structure to obtain complete information on an unsuccessful call to a function.
The following rules are followed to determine the first record within a row. The record with the highest rank is the first record.
Return Codes
Diagnostics
SQLGetDiagField() does not post error values for itself. It uses the following return values to report the outcome of its own execution:
Restrictions
None.
Example
See the README file in the sqllib\samples\cli (or sqllib/samples/cli) subdirectory for a list of appropriate samples.
References