Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Severity and State in raiserror in T-SQL actually used in practice? [closed]

Tags:

sql-server

We use RAISERROR in SQL Server. The syntax is RAISERROR('Some Message.', 16, 1).

What is the use of the parameter values 16 and 1 with RAISERROR() in my example? I searched the internet and found that these parameters are called Severity and State. The documentation tells us some esoteric meanings to these values, but doesn't give us good direction on how to use them or why.

What I want to know is what is meant by Severity and State? How should they typically be used?

like image 787
Sonali Avatar asked May 19 '14 10:05

Sonali


People also ask

What is severity in Raiserror in SQL Server?

When RAISERROR is run with a severity of 11 or higher in a TRY block, it transfers control to the associated CATCH block. The error is returned to the caller if RAISERROR is run: Outside the scope of any TRY block. With a severity of 10 or lower in a TRY block.

What is state in Raiserror SQL?

Is an integer from 0 through 255. Negative values default to 1. Values larger than 255 should not be used. If the same user-defined error is raised at multiple locations, using a unique state number for each location can help find which section of code is raising the errors.

What severity level errors are managed in try catch block?

Errors that have a severity of 20 or higher that stop the SQL Server Database Engine task processing for the session. If an error occurs that has severity of 20 or higher and the database connection is not disrupted, TRY... CATCH will handle the error.

How many error severity levels are there in SQL Server?

The severity level are displayed in the table below. Messages with a severity level of 0 to 10 are informational messages and not actual errors. Severity levels 11 to 16 are generated as a result of user problems and can be fixed by the user.


2 Answers

Error State is there to pin point the location where error occured in your code. Say if you have a 1000 lines long stored procedure and you are raising errors in different places, Error state will help you to tell which error was actually raised.

Error Severity gives information about the type of error that occured,

upto Severity level 10 are informational messages.

11-16 are considered errors that can be fixed by the user.

17-19 are considered Non-Fatal errors in Sql Server Resources, Engine and other stuff .

20-25 are considered Fatal Error which causes sql server to shut down the process immediately.

like image 87
M.Ali Avatar answered Oct 21 '22 11:10

M.Ali


According to the docs on RAISERROR:

Severity

Is the user-defined severity level associated with this message. When using msg_id to raise a user-defined message created using sp_addmessage, the severity specified on RAISERROR overrides the severity specified in sp_addmessage.

Severity levels from 0 through 18 can be specified by any user. Severity levels from 19 through 25 can only be specified by members of the sysadmin fixed server role or users with ALTER TRACE permissions. For severity levels from 19 through 25, the WITH LOG option is required. Severity levels less than 0 are interpreted as 0. Severity levels greater than 25 are interpreted as 25.

State

Is an integer from 0 through 255. Negative values default to 1. Values larger than 255 should not be used.

If the same user-defined error is raised at multiple locations, using a unique state number for each location can help find which section of code is raising the errors.

like image 23
Raj Avatar answered Oct 21 '22 12:10

Raj