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]};
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 whenever sqlerror call err_handler();
exec sql whenever sqlwarning call warn_handler();
long SQLCODE;
exec sql begin declare section;
CS_CHAR lastname[40]; CS_CHAR firstname[20]; CS_CHAR phone[12]; exec sql end declare section;
exec sql declare au_list cursor for
select au_lname, au_fname, phone
from authors
order by au_lname;
exec sql open au_list;
exec sql whenever not found go to list_done;
while (TRUE){ exec sql fetch au_list
into :lastname, :firstname, :phone;
printf(“Lastname is: %s\n”, lastname,
“Firstname is: %s\n”, firstname,
“Phone number is: %s\n”, phone;
}
list_done:
exec sql close au_list;
exec sql disconnect current;
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 10-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. |