Throw Statement in Sql server 2012


In 2012, raise error is replaced by throw statement

If we executed the below script, we will have the raise error statement that will catch error

Raise error have some limitation it will not flow the control to the catch block in some severities. Please execute the below query to see the limitation

For severity 10, it will not flow the control.

So we don’t have error raised.

Throw will eliminate these limitations.

Syntax of throw:

THROW [ { error_number | @local_variable },

{ message | @local_variable },

{ state | @local_variable } ]

[ ; ]

Parameters:

Error_number:

Is a constant or variable that represents the exception. error_number is int and must be greater than or equal to 50000 and less than or equal to 2147483647.

The error_number parameter does not have to be defined in sys.messages

Message:

Is an string or variable that describes the exception. message is nvarchar(2048).

State:

Is a constant or variable between 0 and 255 that indicates the state to associate with the message. state is tinyint.

There is no severity parameter. The exception severity is always set to 16.

We have to specify throw statement inside begin try with parameters

We should not specify throw without parameters in try block

Throw statement should end with semi colon

Leave a comment

Design a site like this with WordPress.com
Get started