Using GET DIAGNOSTICS for Error Checking
GET DIAGNOSTICS returns values from various fields in the diagnostics area. For each field in the diagnostics area that you wish to access, you must supply a host variable of a compatible data type.
The following example illustrates how to use the GET DIAGNOSTICS statement to display error
information. The example shows the error display routine called
disp_sqlstate_err( ):
void disp_sqlstate_err() { int j; EXEC SQL BEGIN DECLARE SECTION; int exception_count; char overflow[2]; int exception_num=1; char class_id[255]; char subclass_id[255]; char message[255]; int messlen; char sqlstate_code[6]; int i; EXEC SQL END DECLARE SECTION; printf("---------------------------------"); printf("-------------------------\n"); printf("SQLSTATE: printf("SQLCODE: %d\n", SQLCODE); printf("\n"); EXEC SQL get diagnostics :exception_count = NUMBER, :overflow = MORE; printf("EXCEPTIONS: Number=%d\t", exception_count); printf("More? %s\n", overflow); for (i = 1; i <= exception_count; i++) { EXEC SQL get diagnostics exception :i :sqlstate_code = RETURNED_SQLSTATE, :class_id = CLASS_ORIGIN, :subclass_id = SUBCLASS_ORIGIN, :message = MESSAGE_TEXT, :messlen = MESSAGE_LENGTH; printf("- - - - - - - - - - - - - - - - - - - -\n"); printf("EXCEPTION %d: SQLSTATE=%s\n", i, sqlstate_code); message[messlen-1] ='\0'; printf("MESSAGE TEXT: %s\n", message); j = stleng(class_id); while((class_id[j] == '\0') || (class_id[j] == ' ')) j--; class_id[j+1] = '\0'; printf("CLASS ORIGIN: j = stleng(subclass_id); while((subclass_id[j] == '\0') || (subclass_id[j] == ' ')) j--; subclass_id[j+1] = '\0'; printf("SUBCLASS ORIGIN: } printf("---------------------------------"); printf("-------------------------\n"); }