Specifies an action to occur whenever an executable SQL statement causes a specified condition.
exec sql whenever {sqlerror | not found | sqlwarning} {continue | go to label | goto label | stop | call routine_name [args]} end-exec
Specifies an action to take when an error is detected, such as a syntax error returned to the Embedded SQL program from SQL Server.
Specifies an action to take when a fetch or select into statement retrieves no data or when a searched update or delete statement affects no rows.
Specifies an action to take when a warning is received; for example, when a character string is truncated.
Take no action when the condition occurs.
Transfer control to the program statement at the specified label.
A host language statement label, such as a C label.
Terminate the Embedded SQL program when the condition occurs.
Transfer control to a callable routine in the program, such as a user-defined function or subroutine.
A host language routine that can be called. The routine must be able to be called from the source file that contains the whenever statement. You may need to declare the routine as external to compile the Embedded SQL program.
One or more arguments to be passed to the callable routine, using the parameter-passing conventions of the host language. The arguments can be any list of host variables, literals, or expressions that the host language allows. A space character should separate each argument from the next.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 LNAME PIC X(15).
01 FNAME PIC X(15).
01 PHONE PIC X(15).
EXEC SQL END DECLARE SECTION END-EXEC.
EXEC SQL WHENEVER SQLERROR PERFORM ERR-PARA END-EXEC.
EXEC SQL WHENEVER SQLWARNING PERFORM WARN-PARA END-EXEC.
* If there are no more records to process from the fetch, stop the
* program.
EXEC SQL WHENEVER NOT FOUND STOP END-EXEC.
...
EXEC SQL DECLARE au_list CURSOR FOR
SELECT au_lname, au_fname, phone
FROM authors
ORDER BY au_lname END-EXEC.
EXEC SQL OPEN au_list END-EXEC.
PERFORM FETCH-LOOP UNTIL SQLCODE = 100 END-EXEC.
EXEC SQL CLOSE au_list END-EXEC.
...
FETCH-LOOP.
EXEC SQL FETCH au_list INTO
:LNAME, :FNAME, :PHONE END-EXEC
DISPLAY "LAST NAME : ",LNAME
DISPLAY "FIRST NAME : ",FNAME
DISPLAY "PHONE : ",PHONE
END-FETCH-LOOP.
WARN-PARA.
DISPLAY "Warning code is " SQLCODE.
DISPLAY "Warning message is " SQLERRMC.
...
WARN-PARA-END.
EXIT.
ERR-PARA.
*
* print the error code, the error message and the line number of
* the command that caused the error.
*
DISPLAY "Error code is " SQLCODE.
DISPLAY "Error message is " SQLERRMC.
EXIT.
The whenever statement causes the Embedded SQL precompiler to generate code following each executable SQL statement. The generated code includes the test for the condition and the host language statement or statements that carry out the specified action.
The Embedded SQL precompiler generates code for the SQL statements that follow the whenever statement in the source file, including SQL statements in subroutines that are defined in the same source file.
Use whenever…continue to cancel a previous whenever statement. The continue action causes the Embedded SQL precompiler to ignore the condition. To prevent infinite loops, use whenever…continue in an error handler before executing any Embedded SQL statements.
When you use whenever…go to label, label must represent a valid location to resume execution. In C, for example, label must be declared in any routine that has executable SQL statements within the scope of the whenever statement. C does not allow a goto statement to jump to a label declared in another function.
If you have a whenever statement in your program but you have not declared SQLCA or SQLSTATE status variables, the Embedded SQL precompiler assumes that you are using the SQLCODE variable. Be sure that SQLCODE is declared. Otherwise, the generated code will not compile.
The following table pertains to the SQL descriptor used for dynamic SQL statements. Sybase’s use of dynamic SQL values conforms to the ANSI/ISO 185-92 SQL-92 standards. For more information, see the appropriate ANSI/ISO documentation.
ANSI SQL datatype |
Code |
Sybase-Defined datatype |
Client-Library code |
|
---|---|---|---|---|
bit |
14 |
smalldatetime |
-9 |
|
character |
1 |
money |
-10 |
|
character varying |
12 |
smallmoney |
-11 |
|
date, time |
9 |
text |
-3 |
|
decimal |
3 |
image |
-4 |
|
double precision |
8 |
tinyint |
-8 |
|
float |
6 |
binary |
-5 |
|
integer |
4 |
varbinary |
-6 |
|
numeric |
2 |
long binary |
-7 |
|
real |
7 |
longchar |
-2 |
|
smallint |
5 |
Value |
Description |
---|---|
type |
The datatype of this column (item number) in the row. For values, see Table 9-5. |
length |
The length, in characters, of the dynamic parameter marker of target for the specified SQL descriptor. |
returned_length |
The length of char types of the values from the select column list. |
precision |
An integer specifying the total number of digits of precision for the CS_NUMERIC variable. |
scale |
An integer specifying the total number of digits after the decimal point for the CS_NUMERIC variable. |
nullable |
Equals 0 if the dynamic parameter marker can accept a null value; otherwise, equals 1. |
indicator |
Value for the indicator parameter associated with the dynamic parameter marker or target. |
data |
Value for the dynamic parameter marker or target associated with the specified SQL descriptor. If indicator is negative, this field is undefined. |
name |
The name of the specified SQL descriptor containing information about the dynamic parameter markers. |