Monday, May 18, 2015

Create Custom Error Message for Error Handle (RAISERROR) in SQL server

It is very easy to create your own Error text in SQL Server Error handler.
We can achive this by using sp_addmessage .

In sp_addmessage there are few parameter (Message number,Severity,Message text,Language,log and replace message)

But as of now we will use following :

exec sp_addmessage @msgnum=50001,@severity=1,@msgtext='This is Custom Error Message created By Pritam'

While giving @msgnum have to remember it should be unique. For that we have to check whether given @msgnum is exists or not.

select max(message_id) from sys.messages

Output:
49913

NB: User-defined error messages must have an message_id greater than 50000.

Now we have to set the @severity.To set this we have to know about severity level (10 to 24).

Follow URL:


Now you can see your custom message in  sys.messages


Now you can call this error message by using:

RAISERROR ( 50001,1,1)

Output:

This is Custom Error Message created By Pritam
Msg 50001, Level 1, State 1

You can use this RAISERROR ( 50001,1,1as Custom Error message.


No comments:

Post a Comment