![]() |
![]() |
|
|
![]() |
![]() |
ALLBASE/SQL FORTRAN Application Programming Guide: HP 9000 Computer Systems > Chapter 5 Runtime Status Checking and the SQLCA![]() The SQLCA COMMON Block |
|
The SQLCA is used for communicating information between the application program and ALLBASE/SQL. SQL places information in the SQLCA each time it is called. Since there is no guarantee that information from one call to SQL will be present after the next call to SQL, any information needed from the SQLCA must be obtained after each call to ALLBASE/SQL. Every ALLBASE/SQL FORTRAN program unit must have the EXEC SQL INCLUDE SQLCA statement before the Host Variable Declaration Section to declare the SQL Communication Area:
The FORTRAN preprocessor generates the following declaration in the modified source file after it parses this SQL command:
The following fields in this record are available for you to use in status checking.
The other fields are reserved for use by ALLBASE/SQL only. As discussed in the chapter, "Host Variables," the SQLCA COMMON block must be included whenever a program unit executes SQL commands. If no EXEC SQL INCLUDE SQLCA statement is included, the FORTRAN preprocessor will issue a warning message. If a program accesses multiple DBEnvironments, each DBEnvironment requires a separate SQLCA. Consequently, ensure that all program units that access the same DBEnvironment are preprocessed separately from any program units that access a different DBEnvironment. SQLCA.SQLCode can contain one of the following values:
Note that when you execute UPDATE or DELETE commands dynamically and no rows qualify for the operation, SQLCode is not set to 100. You can use SQLCA.SQLErrd(3) to detect this condition as discussed later in this chapter. Negative SQLCA.SQLCode values are the same as the numbers associated with their corresponding messages in the ALLBASE/SQL message catalog. For example, the error message associated with an SQLCA.SQLCode of -2613 is:
SQLCA.SQLCode is set by all SQL commands except the following directives:
When SQLCode is -4008, -14024, or a greater negative value than -14024, ALLBASE/SQL automatically rolls back the current transaction. When this condition occurs, ALLBASE/SQL also sets SQLWarn(6) to 'W'. Refer to the discussion later in this chapter on SQLWarn(6) for more information on this topic. More than one SQLCA.SQLCode is returned when more than one error occurs. For example, if you attempt to execute the following SQL command, two negative SQLCA.SQLCode values result:
The SQLCA.SQLCodes associated with the two errors are:
To obtain all SQLCA.SQLCodes associated with the execution of an SQL command, you execute the SQLEXPLAIN command until SQLCA.SQLCode is 0:
The subroutine named SQLStatusCheck is executed when SQLCA.SQLCode is a negative number. Before executing SQLEXPLAIN for the first time, the program has access to the first SQLCA.SQLCode returned. Each time SQLEXPLAIN is subsequently executed, the next SQLCA.SQLCode becomes available to the program, and so on until SQLCA.SQLCode equals zero. If the user needs to have further access to a SQLCA.SQLCode value, the SQLCA.SQLCode value needs to be saved into another data variable. Each time SQLEXPLAIN or any other SQL command is executed, the SQLCA.SQLCode value changes to reflect the result of the previously executed command. This example explicitly tests the value of SQLCA.SQLCode twice: first to determine whether it is equal to 100, then to determine whether it is less than 0. If the value 100 exists, no error will have occurred and the program will display the message, No rows qualify for this operation. It is necessary for the program to display its own message in this case because only negative SQLCA.SQLCodes and the SQLCA.SQLWarn(0) W flag have messages to describe their corresponding conditions. The SQLCA.SQLCode is also used in implicit status checking:
In the following situation, when ALLBASE/SQL detects a negative SQLCA.SQLCode, the code routine at Label 2000 in the same program unit is executed. When ALLBASE/SQL detects an SQLCA.SQLCode of 100, the code routine at Label 4000 in the same program unit is executed instead:
WHENEVER commands remain in effect for all SQL commands that appear sequentially after them in the modified source code until another WHENEVER command for the same condition occurs. The following WHENEVER command, for example, changes the effect of an SQLCA.SQLCode of 100. Instead of the code routine at Label 4000 in the same program unit being executed, the code routine at Label 4500 in the same program unit is executed:
The scope of WHENEVER commands is fully explained later in this chapter under "Implicit Status Checking." SQLCA.SQLErrd(3) can contain one of the following values:
The meaning of any positive SQLCA.SQLErrd(3) value depends on the SQLCA.SQLCode value. When SQLCA.SQLCode is 0, SQLErrd(3) indicates:
A W in SQLCA.SQLWarn(0) in conjunction with a 0 in SQLCA.SQLCode indicates that the SQL command just executed caused a warning condition. Warning conditions flag unusual but not necessarily important conditions. For example, if a program attempts to submit an SQL command that grants an already-existing authority, a message such as the following would be retrieved when SQLEXPLAIN is executed:
In the case of the following warning, the situation may or may not indicate a problem:
This warning occurs when a program submits a RELEASE command without first terminating a transaction with a COMMIT WORK or ROLLBACK WORK command. If the transaction performed no UPDATE, INSERT, or DELETE operations, this situation causes no work to be lost. If the transaction did perform UPDATE, INSERT, or DELETE operations, the database changes are rolled back when the RELEASE command is processed. An error and a warning condition may exist at the same time. In this event, SQLCA.SQLCode is set to a negative number, but SQLCA.SQLWarn(0) is set to W only if SQLCA.SQLWarn(6) is set to W. Messages describing all the warnings and errors can be displayed as follows:
If multiple warnings but no errors result when ALLBASE/SQL processes a command, SQLWarn(0) is set to W and remains set until the last warning message has been retrieved by SQLEXPLAIN or another SQL command is executed. In the following example, DisplayWarning is executed when this condition exists:
When you use the SQLWARNING option of the WHENEVER command, ALLBASE/SQL checks for a W in SQLCA.SQLWarn(0). You can use the WHENEVER command to do implicit status checking equivalent to that done explicitly above as follows:
When a warning condition that sets SQLCA.SQLWarn(0) occurs, SQLCA.SQLCode does not contain a value that describes the warning. Therefore you cannot explicitly evaluate the contents of SQLCA.SQLCode in order to conditionally handle warnings. You can either display the message SQLEXPLAIN retrieves from the ALLBASE/SQL catalog or you can ignore the warning. A W in SQLWarn(1) indicates truncation of at least one character string value when the string was stored in a host variable. Any associated indicator variable is set to the value of the string length before truncation. For example:
If PartName was declared as a character array of 20 bytes, and the PartName column in the PurchDB.Parts table has a length of 30 bytes, then:
A W in SQLWarn(2) indicates that at least one null value was eliminated from the argument set of an aggregrate function. For example:
If any OrderQty values are null:
When an error exists so serious that ALLBASE/SQL has to roll back the current transaction, SQLWarn(6) is set to W. ALLBASE/SQL automatically rolls back transactions when SQLCode is equal to -4008 or is -14024 or less:
When these errors occur, ALLBASE/SQL sets SQLWarn(6) to W, SQLWarn(0) to W, and SQLCode to a negative number. You only need to examine SQLWarn(6) if you want to terminate your program any time ALLBASE/SQL has to roll back the current transaction:
In this example, the program executes subprogram unit SQLStatusCheck when an error occurs. The program terminates whenever SQLWarn(6) is W, but continues if SQLWarn(6) is not W. If a deadlock or a shared memory problem occurs, the contention that caused it may not exist if the transaction is restarted. In this case, you may want to examine both SQLWarn(6) and SQLCode and terminate the program only when SQLCode is less than -14024:
If a deadlock or a shared memory problem occurs, the program displays all the messages, then continues. The program also continues when an error exists but is not serious enough to cause ALLBASE/SQL to roll back the current transaction. In the case of serious errors, however, SQLCode is set to less than -14024, and the program terminates after displaying all the messages. If multiple SQLCodes result when ALLBASE/SQL processes a command that causes the current transaction to be rolled back, SQLWarn(6) is set to W in conjunction with the first available SQLCode. Therefore, if your program needs to examine SQLWarn(6), ensure that you examine it before using SQLEXPLAIN for the second time or it will be reset. If one or more errors are detected before an automatic rollback occurs, the first SQLCode available to your program will not be equal to -4008 or greater than or equal to -14024. However, should one of these conditions occur, the corresponding SQLCode is guaranteed to be the last SQLCode available to your program, since ALLBASE/SQL rolls back the current transaction and does not continue to look for additional errors. You can use this characteristic to construct a test such that a transaction is automatically reapplied behind the program user's back only if a deadlock or a shared memory problem occurs but no other errors were detected first:
At this point, a SELECT command is executed. If an error occurs, and if the first error detected was a deadlock or a shared memory problem, the SELECT command is automatically re-executed as many as three times before the user is notified of the situation. If other errors occurred before the deadlock or shared memory problem, the transaction is not automatically reapplied. If an error with an SQLCode less then -14024 occurred, the program is terminated after the error messages are displayed. |
![]() |
||
![]() |
![]() |
![]() |
|||||||||
|