whenever

Description

Specifies an action to occur whenever an executable SQL statement causes a specified condition.

Syntax

exec sql whenever {sqlerror | not found | sqlwarning} 
 {continue | go to label | goto label |
 stop | call routine_name [args]} end-exec

Parameters

sqlerror

Specifies an action to take when an error is detected, such as a syntax error returned to the Embedded SQL program from Adaptive Server.

not found

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.

sqlwarning

Specifies an action to take when a warning is received; for example, when a character string is truncated.

continue

Take no action when the condition occurs.

go to | goto

Transfer control to the program statement at the specified label.

label

A host language statement label, such as a C label.

stop

Terminate the Embedded SQL program when the condition occurs.

call

Transfer control to a callable routine in the program, such as a user-defined function or subroutine.

routine_name

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.

args

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.

Examples

Example 1

      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.

Usage


SQL descriptor codes

Table 9-5 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.

Table 9-5: SQL descriptor datatype codes

ANSI SQL datatype

Code

bit

14

character

1

character varying

12

date, time

9

decimal

3

double precision

8

float

6

integer

4

numeric

2

real

7

smallint

5

Sybase-defined datatype

Client-Library code

smalldatetime

-9

money

-10

smallmoney

-11

text

-3

image

-4

tinyint

-8

binary

-5

varbinary

-6

long binary

-7

longchar

-2

Table 9-6: SQL descriptor identifier values

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.