THROW (TSQL Statement)

Raises an error. The batch terminates.

Syntax

THROW error_number , message , state

Arguments

error_number
is any number of your choice, in the range [50000, 2147483647]. It is of type int.
message
is the text of the message, of type varchar.
state
is supplementary information you can pass to the error. It is of type tinyint. You can just pass 1, which is a common value. The value of state has no importance for RSQL, but the client can read it.

Remarks

Normally, rcli runs all the batches contained in a script file.

But if the statement THROW in a batch raises an error with state == 127, the RSQL client stops there and won’t run the subsequent batches in the file.

Examples

PRINT 'Hello, I am the first batch';
PRINT 100/0;                                    -- error raised by the division operator
PRINT 'Goodbye from first batch';
GO

PRINT 'I am the second batch';
THROW 50000, 'THROW error', 1;                  -- error raised by THROW
PRINT 'Goodbye from second batch';
GO

PRINT 'I am the third batch';
THROW 50000, 'THROW error with state=127', 127; -- error raised by THROW, with state=127
PRINT 'Goodbye from third batch';
GO

PRINT 'I am the 4th batch';                     -- this batch is not run
GO

PRINT 'I am the 5th batch';                     -- this batch is not run

The result is:

2016/03/30 00:25:32 --- executing batch example.sql:1 ---
Hello, I am the first batch
2016/03/30 00:25:32 12:14 [ERROR_ARITH/ERROR_SQLDATA_INT_DIVIDE_BY_ZERO/ERROR_BATCH_ABORT/1]
<Arithmetic error. INT division by 0.>
2016/03/30 00:25:32 return code: -1

2016/03/30 00:25:32 --- executing batch example.sql:15 ---
I am the second batch
2016/03/30 00:25:32 3:5 [ERROR_GENERAL/50000/ERROR_BATCH_ABORT/1]
<THROW error>
2016/03/30 00:25:32 return code: -1

2016/03/30 00:25:32 --- executing batch example.sql:20 ---
I am the third batch
2016/03/30 00:25:32 3:5 [ERROR_GENERAL/50000/ERROR_BATCH_ABORT/127]
<THROW error with state=127>
2016/03/30 00:25:32 error state 127: stop processing subsequent batches in this file.
2016/03/30 00:25:32 return code: -1