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 they encounter an error, returning SQLSTATE and SQLCODE values to the calling environment.

You can build explicit error handling into Watcom-SQL stored procedures using the EXCEPTION statement, or you can instruct the procedure to continue execution at the next statement when it encounters an error, using the ON EXCEPTION RESUME statement.

When a Transact-SQL dialect procedure encounters an error, 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'

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

Built-in procedure return values

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

Nonfatal 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 other integers, with their own user-defined meanings.