DB2 Version 10.1 for Linux, UNIX, and Windows

SQLGetDiagRec function (CLI) - Get multiple fields settings of diagnostic record

Returns the current values of multiple fields of a diagnostic record that contains error, warning, and status information. Unlike SQLGetDiagField(), which returns one diagnostic field per call, SQLGetDiagRec() returns several commonly used fields of a diagnostic record: the SQLSTATE, native error code, and error message text.

Specification:

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

Syntax

SQLRETURN   SQLGetDiagRec (
               SQLSMALLINT       HandleType,       /* fHandleType */
               SQLHANDLE         Handle,           /* hHandle */
               SQLSMALLINT       RecNumber,        /* iRecNumber */
               SQLCHAR           *SQLState,        /* *pszSqlState */
               SQLINTEGER        *NativeErrorPtr,  /* *pfNativeError */
               SQLCHAR           *MessageText,     /* *pszErrorMsg */
               SQLSMALLINT       BufferLength,     /* cbErrorMsgMax */
               SQLSMALLINT       *TextLengthPtr);  /* *pcbErrorMsg */

Function arguments

Table 1. SQLGetDiagRec arguments
Data type Argument Use Description
SQLSMALLINT HandleType input A handle type identifier that describes the type of handle for which diagnostics are desired. 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.
SQLCHAR * SQLState output Pointer to a buffer in which to return 5 characters plus a NULL terminator for the SQLSTATE code pertaining to the diagnostic record RecNumber. The first two characters indicate the class; the next three indicate the subclass.
SQLINTEGER * NativeErrorPtr output Pointer to a buffer in which to return the native error code, specific to the data source.
SQLCHAR * MessageText output Pointer to a buffer in which to return the error message text. The fields returned by SQLGetDiagRec() are contained in a text string.
SQLINTEGER BufferLength input Number of SQLCHAR elements (or SQLWCHAR elements for the Unicode variant of this function) needed to store the MessageText buffer.
SQLSMALLINT * TextLengthPtr output Pointer to a buffer in which to return the total number of SQLCHAR elements (or SQLWCHAR elements for the Unicode variant of this function), excluding the null-termination character, available to return in *MessageText. If the number of SQLCHAR or SQLWCHAR elements available to return is greater than BufferLength, then the error message text in *MessageText is truncated to BufferLength minus the length of a null-termination character.

Usage

An application typically calls SQLGetDiagRec() when a previous call to a CLI function has returned anything other than SQL_SUCCESS. However, any function can post zero or more errors each time it is called, so an application can call SQLGetDiagRec() after any function call. An application can call SQLGetDiagRec() multiple times to return some or all of the records in the diagnostic data structure.

SQLGetDiagRec() returns a character string containing the following fields of the diagnostic data structure record:
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_SQLSTATE (return type CHAR *)
A five-character SQLSTATE diagnostic code.

SQLGetDiagRec() cannot be used to return fields from the header of the diagnostic data structure (the RecNumber argument must be greater than 0). The application should call SQLGetDiagField() for this purpose.

SQLGetDiagRec() retrieves only the diagnostic information most recently associated with the handle specified in the Handle argument. If the application calls another function, except SQLGetDiagRec() or SQLGetDiagField(), any diagnostic information from the previous calls on the same handle is lost.

An application can scan all diagnostic records by looping, incrementing RecNumber, as long as SQLGetDiagRec() returns SQL_SUCCESS. Calls to SQLGetDiagRec() are non-destructive to the header and record fields. The application can call SQLGetDiagRec() again at a later time to retrieve a field from a record, as long as no other function, except SQLGetDiagRec() or SQLGetDiagField(), has been called in the interim. The application can call SQLGetDiagField() to retrieve the value of the SQL_DIAG_NUMBER field, which is the total number of diagnostic records available. SQLGetDiagRec() should then be called that many times.

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 list of header and record fields for the DiagIdentifier argument.

Return codes

Diagnostics

SQLGetDiagRec() 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: The *MessageText buffer was too small to hold the requested diagnostic message. No diagnostic records were generated. 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:
    • RecNumber was negative or 0.
    • 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.

Example

  /* get multiple fields settings of diagnostic record */
  SQLGetDiagRec(SQL_HANDLE_STMT,
                hstmt,
                1,
                sqlstate,
                &sqlcode,
                message,
                200,
                &length);