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:

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

Table A-2 describes the built-in procedure return values and their meanings:

Table A-2: Built-in procedure return values




Procedure executed without error


Missing object


Data type error


Process was chosen as deadlock victim


Permission error


Syntax error


Miscellaneous user error


Resource error, such as out of space


Nonfatal internal problem


System limit was reached


Fatal internal inconsistency


Fatal internal inconsistency


Table or index is corrupt


Database is corrupt


Hardware error

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