|
The SQLCA and message vector are two separate host structures that SQL declares when it precompiles an INCLUDE SQLCA statement.
Both the SQLCA and the message vector provide ways of handling errors:
SQL updates the contents of the SQLCA and the message vector after completion of every executable SQL statement (nonexecutable statements are the DECLARE, WHENEVER, and INCLUDE statements).
You do not have to use the INCLUDE SQLCA statement in programs. However, if you do not, you must explicitly declare the SQLCODE parameter to receive values from SQL. SQLCODE is explicitly declared as an unscaled, signed longword integer.
SQLCODE is a deprecated feature of the ANSI/ISO SQL standard and is replaced by SQLSTATE. To comply with the ANSI/ISO SQL standard, you should explicitly declare either SQLCODE or, preferably, SQLSTATE instead of using the INCLUDE SQLCA statement. SQLCA (and the INCLUDE SQLCA statement) is not part of the ANSI/ISO SQL standard. If you declare SQLCODE or SQLSTATE but use the INCLUDE SQLCA statement, SQL uses the SQLCA.
Programs that do not use the INCLUDE SQLCA statement will not have the message vector declared by the precompiler. Such programs must explicitly declare the message vector if they:
The message vector is not part of the ANSI/ISO SQL standard.
When the SQLCA structure is explicitly declared by a program, SQL does not update the SQLERRD fields. If you want the SQLERRD fields updated, include the SQLCA definitions in the program using the EXEC SQL INCLUDE SQLCA statement.
Section B.1 and Section B.2 describe the SQLCA and the message
vector in more detail. Section B.3 shows the declarations SQL makes
for them in different host language programs.
B.1 The SQLCA
The only fields of interest in the SQLCA are the SQLCODE field and the second through sixth elements of the SQLERRD array.
Example B-1 shows the interactive SQL display for the SQLCA after the "attempt to fetch past end of stream" error.
Example B-1 Fields in the SQLCA
SQL> SHOW SQLCA
SQLCA:
SQLCAID: SQLCA SQLCABC: 128
SQLCODE: 100
SQLERRD: [0]: 0
[1]: 0
[2]: 0
[3]: 0
[4]: 0
[5]: 0
SQLWARN0: 0 SQLWARN1: 0 SQLWARN2: 0
SQLWARN3: 0 SQLWARN4: 0 SQLWARN5: 0
SQLWARN6: 0 SQLWARN7: 0
SQLSTATE: 02000
SQLSTATE is not part of the SQLCA, although it appears in the display.
The remainder of this section describes the fields of the SQLCA.
SQLCAID
An 8-character field whose value is always the character string SQLCA. It is provided for compatibility with DB2 databases. The FORTRAN SQLCA does not include this field.SQLCABC
An integer field whose value is always the length, in bytes, of the SQLCA. It is provided for compatibility with DB2 databases. The value is always 128. The FORTRAN SQLCA does not include this field.SQLCODE
An integer field whose value indicates the error status returned by the most recently executed SQL statement. A positive value other than 100 indicates a warning, a negative value indicates an error, and a zero indicates successful execution.Table B-1 shows the possible numeric and literal values that SQL returns to the SQLCODE field and explains the meaning of the values.
Table B-1 Values Returned to the SQLCODE Field Numeric Value Literal Value Meaning Success Status Code 0 SQLCODE_SUCCESS Statement completed successfully. Warning Status Codes 100 SQLCODE_EOS SELECT statement or cursor came to the end of stream. 1003 SQLCODE_ELIM_NULL¹ Null value was eliminated in a set function. 1004 SQLCODE_TRUN_RTRV¹ String truncated during assignment. This occurs only during data retrieval. Error Status Codes --1 SQLCODE_RDBERR Oracle Rdb returned an error. The value of --1 is a general error SQLCODE value returned by any error not corresponding to the other values in this table. Use sql_signal or sql_get_error_text to return a meaningful error. --304 SQLCODE_OUTOFRAN Value is out of range for a host variable. --305 SQLCODE_NULLNOIND Tried to store a null value into a host language variable with no indicator variable. --306 SQLCODE_STR_DAT_TRUNC¹ String data, right truncation. --307 SQLCODE_INV_DATETIME Date-time format is invalid. --501 SQLCODE_CURNOTOPE Cursor is not open. --502 SQLCODE_CURALROPE Cursor is already open. --507 SQLCODE_UDCURNOPE Cursor in an UPDATE or DELETE operation is not opened. --508 SQLCODE_UDCURNPOS Cursor in an UPDATE or DELETE operation is not positioned on a row. --509 SQLCODE_UDCURDEL Cursor in an UPDATE or DELETE operation is positioned on a deleted row. --803 SQLCODE_NO_DUP Updating would cause duplication on a unique index. --811 SQLCODE_SELMORVAL The result of a singleton select returned more than one value. --817 SQLCODE_ROTXN Attempt to update from a read-only transaction. --913 SQLCODE_DEADLOCK Request failed due to resource deadlock. --1001 SQLCODE_INTEG_FAIL Constraint failed. --1002 SQLCODE_NOT_VALID Valid-if failed. --1003 SQLCODE_LOCK_CONFLICT NO WAIT request failed because resource was locked. --1004 SQLCODE_BAD_TXN_STATE Invalid transaction state--the transaction already started. --1005 SQLCODE_NO_TXN No transaction active. --1006 SQLCODE_BAD_VERSION Version of the underlying system does not support a feature that this query uses. --1007 SQLCODE_TRIG_ERROR Trigger forced an error. --1008 SQLCODE_NOIMPTXN No implicit distributed transaction outstanding. --1009 SQLCODE_DISTIDERR Distributed transaction ID error. --1010 SQLCODE_BAD_CTX_VER Version field in the context structure is defined incorrectly. --1011 SQLCODE_BAD_CTX_TYPE Type field in the context structure is defined incorrectly. --1012 SQLCODE_BAD_CTX_LEN Length field in the context structure is defined incorrectly. --1013 SQLCODE_BASROWDEL Row that contains the list has been deleted. --1014 SQLCODE_DIFFDEFINV Invoker of the module is not the same as the definer (the user who compiled the module). --1015 SQLCODE_STMTNOTPRE Dynamic statement is not prepared. --1016 SQLCODE_NOSUCHCONN Connection does not exist. --1017 SQLCODE_CONNAMEXI Connection name already exists. --1018 SQLCODE_DBENVSYNERR Database environment specification contains a syntax error. --1019 SQLCODE_DBSPECSYNERR Database specification contains a syntax error. --1020 SQLCODE_ATTACHERR Error attaching to the database. --1021 SQLCODE_NOSUCHALIAS Alias is not known. --1022 SQLCODE_ALIASINUSE Alias is already declared. --1023 SQLCODE_COLEXISTS Column already exists in the table. --1024 SQLCODE_COLNOTDEF Column not defined in the table. --1025 SQLCODE_TBLEXISTS Table already exists in the database or schema. --1026 SQLCODE_DOMEXISTS Domain already exists in the database or schema. --1027 SQLCODE_DOMNOTDEF Domain is not defined in the database or schema. --1028 SQLCODE_NO_PRIV No privilege for attempted operation. --1029 SQLCODE_BAD_LENGTH Negative length specified for a column. --1030 SQLCODE_BAD_SCALE Negative scale specified for a column. --1031 SQLCODE_RO_TABLE Attempt to update a read-only table. --1032 SQLCODE_OBSMETADATA Metadata no longer exists. --1033 SQLCODE_UNRES_REL Table is not reserved in the transaction. --1034 SQLCODE_CASENOTFND Case not found; WHEN not specified. --1035 SQLCODE_CHKOPT_VIOL Integer failure with check option. --1036 SQLCODE_UNTERM_C_STR Unterminated C string. --1037 SQLCODE_INDIC_OVFLOW Indicator overflow. --1038 SQLCODE_INV_PARAM_VAL Invalid parameter value. --1039 SQLCODE_NULL_ELIMIN Null eliminated in the set function. --1040 SQLCODE_INV_ESC_SEQ Invalid escape sequence. --1041 SQLCODE_RELNOTDEF Table not defined in the database or schema.
¹Only the SQL92 dialect returns this value.
Programs can use the literal values to check for success, the end of record stream warnings, or specific errors. Your program can check for particular error codes and execute different sets of error-handling statements depending upon the error code returned. However, because the values in Table B-1 do not reflect all the possible errors or warnings, your program should check for any negative value.
SQL inserts the RDB message vector (see Section B.2) along with the SQLCA structure when it executes an SQL statement.
Also, string truncation conditions are only reported when the dialect is set to SQL92 prior to a database attach in interactive SQL or when your application is compiled. For example:
SQL> SET DIALECT 'SQL92'; SQL> ATTACH 'FILENAME mf_personnel'; SQL> DECLARE :ln CHAR(10); SQL> SELECT last_name INTO :ln FROM employees WHERE employee_id = '00164'; %RDB-I-TRUN_RTRV, string truncated during assignment to a variable or parameter SQL> SHOW SQLCA SQLCA: SQLCAID: SQLCA SQLCABC: 128 SQLCODE: 1004 SQLERRD: [0]: 0 [1]: 0 [2]: 1 [3]: 0 [4]: 0 [5]: 0 SQLWARN0: 0 SQLWARN1: 0 SQLWARN2: 0 SQLWARN3: 0 SQLWARN4: 0 SQLWARN5: 0 SQLWARN6: 0 SQLWARN7: 0 SQLSTATE: 01004 %RDB-I-TRUN_RTRV, string truncated during assignment to a variable or parameterFor each language, SQL provides a file that contains the declarations of all the error literals shown in Table B-1. You can include this file in precompiled SQL and module language programs.
Table B-2 shows how to include this file in your program.
Table B-2 Including the Error Literals File in Programs Precompiled or Module Language Declaration Ada with SQL_SQLCODE;
with SQL_SQLDA;
with SQL_SQLDA2; ¹BASIC %INCLUDE "sys$library:sql_literals.bas" C #include "sys$library:sql_literals.h" COBOL COPY 'SYS$LIBRARY:SQL_LITERALS' FORTRAN INCLUDE 'SYS$LIBRARY:SQL_LITERALS.FOR' Pascal %include 'sys$library:sql_literals.pas' PL/I %INCLUDE 'sys$library:sql_literals.pli';
¹You must compile the Ada package, SYS$LIBRARY:SQL_LITERALS.ADA, before you use it in a program. Only declare SQL_SQLDA and SQL_SQLDA2 when you use dynamic SQL.
In addition to the error literals, the file contains declarations for the SQLTYPE field in the SQLDA. See Appendix D for information about the SQLTYPE field.
Example B-2 shows how to include the error literals file in a COBOL program.
Example B-2 Including Error Literals in a COBOL Program
IDENTIFICATION DIVISION. PROGRAM-ID. LITERAL-TESTS. * * This program tests the use of symbolic literals for SQLCODE and * SQLDA_DATATYPE. All the literal definitions are part of a file that * is used with the COPY command. * DATA DIVISION. WORKING-STORAGE SECTION. COPY SQL_LITERALS. EXEC SQL INCLUDE SQLCA END-EXEC. 01 CDE PIC X(5). 01 DISP_SQLCODE PIC S9(9) DISPLAY SIGN LEADING SEPARATE. 01 GETERRVARS. 02 error-buffer-len PIC S9(9) COMP VALUE 132. 02 error-msg-len PIC S9(9) COMP. 02 error-buffer PIC X(132). exec sql whenever sqlerror continue end-exec. PROCEDURE DIVISION. * * test for sqlcode -501 SQLCODE_CURNOTOPE * exec sql declare A cursor for select college_code from colleges where college_name like 'D%' order by 1 end-exec. exec sql fetch A into :CDE end-exec. if sqlcode = SQLCODE_CURNOTOPE then MOVE sqlcode to DISP_SQLCODE DISPLAY "SQLCODE after attempt to fetch is ", DISP_SQLCODE CALL "sql_get_error_text" USING BY REFERENCE error-buffer, BY VALUE error-buffer-len, BY REFERENCE error-msg-len. DISPLAY BUFFER(1:error-msg-len) end-if. exec sql close A end-exec. * * test for SQLCODE 0 SQLCODE_SUCCESS * exec sql insert into employees (employee_id, last_name, sex) values ('00999','Jones','M') end-exec. if sqlcode = SQLCODE_SUCCESS then MOVE sqlcode to DISP_SQLCODE DISPLAY "SQLCODE after insert is ", DISP_SQLCODE CALL "sql_get_error_text" USING BY REFERENCE error-buffer, BY VALUE error-buffer-len, BY REFERENCE error-msg-len. DISPLAY BUFFER(1:error-msg-len) end-if. EXEC SQL ROLLBACK END-EXEC. STOP RUN.
SQLERRM
The SQLERRM is a structure containing two fields: a word field called SQLERRML and a 70-character field called SQLERRMC. It is provided only for compatibility with DB2 software.SQLERRD[x]
A zero-based array of six integers. The only elements of the array that SQL uses are the second through sixth elements (SQLERRD[1], SQLERRD[2], SQLERRD[3], SQLERRD[4] and SQLERRD[5] in the display from SHOW SQLCA). The remainder of the elements are provided for compatibility with DB2 software.When you use dynamic SQL, SQL puts a value in the second element (SQLERRD[1]) after SQL executes the DESCRIBE statement. The values represent the following:
- 0: The statement is any SQL statement except a SELECT statement or CALL statement.
- 1: The statement is a SELECT statement.
- 2: The statement is a CALL statement.
SQL puts a value in the third element (SQLERRD[2]) after successful execution of the following statements:
- INSERT: The number of rows stored by the statement.
- UPDATE: The number of rows modified by the statement.
- DELETE: The number of rows deleted by the statement.
- FETCH: The number of the row on which the cursor is currently positioned.
- SELECT: The number of rows in the result table formed by the SELECT statement. (Note: The SQLERRD[2] field is not updated for dynamic SELECT statements.)
SQL puts the following values in the third and fourth elements after successful execution of an OPEN statement for a table cursor:
- SQLERRD[2]: Estimated result table cardinality
- SQLERRD[3]: Estimated I/O operations
You must recompile application modules so that the new values in SQLERRD[2] and SQLERRD[3] can be returned.
SQL puts the following values in the second, fourth, fifth, and sixth elements after successful execution of an OPEN statement that opens a list cursor:
- SQLERRD[1]: Longword length of the longest actual segment
- SQLERRD[3]: Longword number of segments
- SQLERRD[4,5]: Two contiguous longwords contained a quadword number of total bytes
SQL puts no meaningful data in the sixth element of the SQLERRD array after successful execution of a FETCH statement.
SQLERRD[1] on a LIST cursor fetch returns the segment size in octets.
After error statements or any other cases, the value of SQLERRD is undefined.
SQLWARNx
A series of 1-character fields, numbered from 0 through 7, that SQL does not use. Provided for compatibility with DB2 software.
When SQL precompiles a program, it declares a host structure for the message vector immediately following the SQLCA. It calls the structure RDB$MESSAGE_VECTOR.
Programs most often use the message vector in two ways:
Figure B-1 summarizes the fields of the message vector.
Figure B-1 Fields of the Message Vector
This section shows the SQLCA and message vector declarations for the
host languages supported by the SQL precompiler and module processor.
Example B-3 shows the Ada SQLCA and message vector declaration.
Example B-3 Ada SQLCA and Message Vector Declaration
Package SQL_ADA_CURSOR is
TYPE SQL_TYPE_1 IS NEW STRING(1..6);
type SQLERRM_REC is
record
SQLERRML : short_integer;
SQLERRMC : string (1..70);
end record;
type SQLERRD_ARRAY is array (1..6) of integer;
type SQLCA is
record
SQLCAID : string (1..8) := "SQLCA ";
SQLABC : integer := 128;
SQLCODE : integer;
SQLERRM : sqlerrm_rec;
SQLERRD : sqlerrd_array;
SQLWARN0 : character := ' ';
SQLWARN1 : character := ' ';
SQLWARN2 : character := ' ';
SQLWARN3 : character := ' ';
SQLWARN4 : character := ' ';
SQLWARN5 : character := ' ';
SQLWARN6 : character := ' ';
SQLWARN7 : character := ' ';
SQLEXT : string (1..8) := " ";
end record;
RDB_MESSAGE_VECTOR : SYSTEM.UNSIGNED_LONGWORD_ARRAY(1..20);
pragma PSECT_OBJECT(RDB_MESSAGE_VECTOR,"RDB$MESSAGE_VECTOR");
Example B-4 shows the BASIC SQLCA and message vector declaration.
Example B-4 BASIC SQLCA and Message Vector Declaration
RECORD SQLCA_REC
string SQLCAID = 8
long SQLCABC
long SQLCODE
GROUP SQLERRM
word SQLERRML
string SQLERRMC = 70
END GROUP SQLERRM
long SQLERRD(5)
string SQLWARN0 = 1
string SQLWARN1 = 1
string SQLWARN2 = 1
string SQLWARN3 = 1
string SQLWARN4 = 1
string SQLWARN5 = 1
string SQLWARN6 = 1
string SQLWARN7 = 1
string SQLEXT = 8
END RECORD SQLCA_REC
DECLARE SQLCA_REC SQLCA
RECORD RDB$MESSAGE_VECTOR_REC
long RDB$LU_NUM_ARGUMENTS
long RDB$LU_STATUS
GROUP RDB$ALU_ARGUMENTS(17) ! Arrays in BASIC are always relative
long RDB$LU_ARGUMENT ! to 0. There are 18 array elements.
END GROUP RDB$ALU_ARGUMENTS
END RECORD RDB$MESSAGE_VECTOR_REC
COMMON (RDB$MESSAGE_VECTOR) &
RDB$MESSAGE_VECTOR_REC RDB$MESSAGE_VECTOR
Example B-5 shows the C SQLCA and message vector declaration.
Example B-5 C SQLCA and Message Vector Declaration
struct
{
char SQLCAID[8];
int SQLCABC;
int SQLCODE;
struct {
short SQLERRML;
char SQLERRMC[70];
} SQLERRM;
int SQLERRD[6];
struct {
char SQLWARN0[1];
char SQLWARN1[1];
char SQLWARN2[1];
char SQLWARN3[1];
char SQLWARN4[1];
char SQLWARN5[1];
char SQLWARN6[1];
char SQLWARN7[1];
} SQLWARN;
char SQLEXT[8];
} SQLCA = { "SQLCA ",
128, 0,
{0, ""},
{0,0,0,0,0,0},
{"", "", "", "", "", "", "", ""},
"" };
extern
struct Rdb$MESSAGE_VECTOR_str
RDB$MESSAGE_VECTOR;
|
Copyright ©1996, Oracle Corporation All rights reserved.