DB2 Version 10.1 for Linux, UNIX, and Windows

SQLGetDiagField function (CLI) - Get a field of diagnostic data

Returns the current value of a field of a diagnostic data structure, associated with a specific handle, that contains error, warning, and status information.

Specification:

Unicode equivalent: This function can also be used with the Unicode character set. The corresponding Unicode function is SQLGetDiagFieldW(). See Unicode functions (CLI) for information about ANSI to Unicode function mappings.

Syntax

SQLRETURN   SQLGetDiagField (
               SQLSMALLINT       HandleType,         /* fHandleType */
               SQLHANDLE         Handle,             /* hHandle */
               SQLSMALLINT       RecNumber,          /* iRecNumber */
               SQLSMALLINT       DiagIdentifier,     /* fDiagIdentifier */
               SQLPOINTER        DiagInfoPtr,        /* pDiagInfo */
               SQLSMALLINT       BufferLength,       /* cbDiagInfoMax */
               SQLSMALLINT       *StringLengthPtr);  /* *pcgDiagInfo */

Function arguments

Table 1. SQLGetDiagField arguments
Data type Argument Use Description
SQLSMALLINT HandleType input A handle type identifier that describes the type of handle for which diagnostics are required. The handle type identifier include:
  • SQL_HANDLE_ENV
  • SQL_HANDLE_DBC
  • SQL_HANDLE_STMT
  • SQL_HANDLE_DESC
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 DiagIdentifier is ODBC-defined diagnostic:
  • If DiagInfoPtr points to a character string or binary buffer, BufferLength should be the length of *DiagInfoPtr.
  • If *DiagInfoPtr is an integer, BufferLength is ignored.
  • If *DiagInfoPtr is a Unicode string, BufferLength must be an even number.
If DiagIdentifier is a CLI diagnostic:
  • If *DiagInfoPtr is a pointer to a character string, BufferLength is the number of bytes needed to store the string, or SQL_NTS.
  • If *DiagInfoPtr is a pointer to a binary buffer, then the application places the result of the SQL_LEN_BINARY_ATTR(length) macro in BufferLength. This places a negative value in BufferLength.
  • If *DiagInfoPtr is a pointer to a value other than a character string or binary string, then BufferLength should have the value SQL_IS_POINTER.
  • If *DiagInfoPtr contains a fixed-length data type, then BufferLength is SQL_IS_INTEGER, SQL_IS_UINTEGER, SQL_IS_SMALLINT, or SQL_IS_USMALLINT, as appropriate.
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 or equal to 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:
  1. To obtain specific error or warning information when a function call has returned the SQL_ERROR or SQL_SUCCESS_WITH_INFO (or SQL_NEED_DATA for the SQLBrowseConnect() function) return codes.
  2. To find out the number of rows in the data source that were affected when insert, delete, or update operations were performed with a call to SQLExecute(), SQLExecDirect(), SQLBulkOperations(), or SQLSetPos() (from the SQL_DIAG_ROW_COUNT header field), or to find out the number of rows that exist in the current open static scrollable cursor (from the SQL_DIAG_CURSOR_ROW_COUNT header field).
  3. To determine which function was executed by a call to SQLExecDirect() or SQLExecute() (from the SQL_DIAG_DYNAMIC_FUNCTION and SQL_DIAG_DYNAMIC_FUNCTION_CODE header fields).

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

No CLI-specific header diagnostic field should be associated with an environment handle.

DiagIdentifier argument

This argument indicates the identifier of the field required 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 it contains data pertaining to the function call that returned the diagnostic information, not the specific information. Refer to the list of header and record fields for the DiagIdentifier argument for further information.

Sequence of status records

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:
  • Records that do not correspond to any row appear in front of records that correspond to a particular row, since SQL_NO_ROW_NUMBER is defined to be -1.
  • Records for which the row number is unknown appear in front of all other records, since SQL_ROW_NUMBER_UNKNOWN is defined to be -2.
  • For all records that pertain to specific rows, records are sorted by the value in the SQL_DIAG_ROW_NUMBER field. All errors and warnings of the first row affected are listed, then all errors and warnings of the next row affected, and so on.

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 about 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.
  • Errors. Status records that describe errors have the highest rank. The following rules are followed to sort errors:
    • Records that indicate a transaction failure or possible transaction failure outrank all other records.
    • If two or more records describe the same error condition, then SQLSTATEs defined by the X/Open CLI specification (classes 03 through HZ) outrank ODBC- and driver-defined SQLSTATEs.
  • Implementation-defined No Data values. Status records that describe CLI No Data values (class 02) have the second highest rank.
  • Warnings. Status records that describe warnings (class 01) have the lowest rank. If two or more records describe the same warning condition, then warning SQLSTATEs defined by the X/Open CLI specification outrank ODBC- and driver-defined SQLSTATEs.

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:
  • SQL_SUCCESS: The function successfully returned diagnostic information.
  • SQL_SUCCESS_WITH_INFO: *DiagInfoPtr was too small to hold the requested diagnostic field so the data in the diagnostic field was truncated. To determine that a truncation occurred, the application must compare BufferLength to the actual number of bytes available, which is written to *StringLengthPtr.
  • SQL_INVALID_HANDLE: The handle indicated by HandleType and Handle was not a valid handle.
  • SQL_ERROR: Possible causes are:
    • The DiagIdentifier argument was not one of the valid values.
    • The DiagIdentifier argument was SQL_DIAG_CURSOR_ROW_COUNT, SQL_DIAG_DYNAMIC_FUNCTION, SQL_DIAG_DYNAMIC_FUNCTION_CODE, or SQL_DIAG_ROW_COUNT, but Handle was not a statement handle.
    • The RecNumber argument was negative or 0 when DiagIdentifier indicated a field from a diagnostic record. RecNumber is ignored for header fields.
    • The value requested was a character string and BufferLength was less than zero.
  • SQL_NO_DATA: RecNumber was greater than the number of diagnostic records that existed for the handle specified in Handle. The function also returns SQL_NO_DATA for any positive RecNumber if there are no diagnostic records for Handle.

Restrictions

None.