Raise custom error message with RAISERROR in SQL Server

Cameron Castillo picture Cameron Castillo · Apr 11, 2013 · Viewed 37.6k times · Source

In previous versions we raised errors in t-sql like:

RAISERROR 50000 'My Error Message'

In the latest SQL Server this syntax has been discontinued and replace with the RaiseError () syntax.

I would like to have a generic method of raising errors, and the best I could come up so far is:

sp_addmessage @msgnum = 50001,
              @severity = 10,
              @msgtext = N'My Error Message', @replace = 'REPLACE';
RAISERROR (50001, 10, 1, 'This error message is not displayed')

But I can't go and create a error message with sp_addmessage for every message, because there are 1000's.

What is the better way to raise messages with a custom message?

Answer

Cameron Castillo picture Cameron Castillo · Apr 11, 2013

This seems to work:

RAISERROR('My Error Message',0,1)