Collection Contents Index Variables in Transact-SQL procedures CHAPTER 31.  Adaptive Server Anywhere as an Open Server pdf/chap30.pdf

User's Guide
   PART 5. The Adaptive Server Family
     CHAPTER 30. Transact-SQL Compatibility       

Error handling in Transact-SQL procedures


Default procedure error handling is different in the Watcom-SQL and Transact-SQL dialects. By default, Watcom-SQL dialect procedures exit when an error is encountered, returning SQLSTATE and SQLCODE values to the calling environment.

Explicit error handling can be built into Watcom-SQL stored procedures using the EXCEPTION statement, or the procedure can be instructed by the ON EXCEPTION RESUME statement to continue execution at the next statement when an error is encountered.

When an error is encountered in a Transact-SQL dialect procedure, execution continues at the following statement. The global variable @@error holds the error status of the most recently executed statement. You can check this variable following a statement to force return from a procedure. For example, the following statement causes an exit if an error occurs.

IF @@error != 0 RETURN

When the procedure completes execution, a return value indicates the success or failure of the procedure. This return status is an integer, and can be accessed as follows:

DECLARE @status INT
EXECUTE @status = proc_sample
IF @status = 0
   PRINT 'procedure succeeded'
ELSE
   PRINT 'procedure failed'

The following table describes the built-in procedure return values and their meanings:

Value

Meaning

0

Procedure executed without error

-1

Missing object

-2

Data type error

-3

Process was chosen as deadlock victim

-4

Permission error

-5

Syntax error

-6

Miscellaneous user error

-7

Resource error, such as out of space

-8

Non-fatal internal problem

-9

System limit was reached

-10

Fatal internal inconsistency

-11

Fatal internal inconsistency

-12

Table or index is corrupt

-13

Database is corrupt

-14

Hardware error

The RETURN statement can be used to return integers other than these, with their own user-defined meanings.

Top of page  Using the RAISERROR statement in procedures

The RAISERROR statement is a Transact-SQL statement for generating user-defined errors. It has a similar function to the SIGNAL statement.

For Info     For a description of the RAISERROR statement, see RAISERROR statement.

By itself, the RAISERROR statement does not cause an exit from the procedure, but it can be combined with a RETURN statement or a test of the @@error global variable to control execution following a user-defined error.

If you set the CONTINUE_AFTER_RAISERROR database option to ON, the RAISERROR statement no longer signals an execution-ending error. Instead, the RAISERROR status code and message are stored and the most recent RAISERROR is returned when the procedure completes. If the procedure that caused the RAISERROR was called from another procedure, the RAISERROR is not returned until the outermost calling procedure terminates.

Intermediate RAISERROR statuses and codes are lost after the procedure terminates. If at return time an error occurs along with the RAISERROR then the error information is returned and the RAISERROR information is lost. The application can query intermediate RAISERROR statuses by examining @@error global variable at different execution points.

Top of page  Transact-SQL-like error handling in the Watcom-SQL dialect

You can make a Watcom-SQL dialect procedure handle errors in a Transact-SQL-like manner by supplying the ON EXCEPTION RESUME clause to the CREATE PROCEDURE statement:

CREATE PROCEDURE sample_proc()
ON EXCEPTION RESUME
BEGIN
   ...
END 

Explicit exception handling code is not executed if an ON EXCEPTION RESUME clause is present.

Top of page  

Collection Contents Index Variables in Transact-SQL procedures CHAPTER 31.  Adaptive Server Anywhere as an Open Server pdf/chap30.pdf