Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise custom error message with RAISERROR in SQL Server

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?

like image 217
Cameron Castillo Avatar asked Apr 11 '13 08:04

Cameron Castillo


People also ask

How do I create a custom error message in SQL Server?

Therefore, when a system error occurs, SQL Server will log a system error and may take actions to fix the error. Custom errors, on the other hand, are generated by T-SQL custom codes based on your code or business logic. To add a custom error message to sys. messages, the stored procedure sp_addmessage is used.

How can increase error message in SQL Server?

The following code example shows how to use RAISERROR inside a TRY block to cause execution to jump to the associated CATCH block. It also shows how to use RAISERROR to return information about the error that invoked the CATCH block. RAISERROR only generates errors with state from 1 through 127.

How can we increase error in stored procedure?

RAISERROR is used to return messages back to applications using the same format as a system error or warning message generated by the SQL Server Database Engine. RAISERROR can return either: A user-defined error message that has been created using the sp_addmessage system stored procedure.


3 Answers

This seems to work:

RAISERROR('My Error Message',0,1)
like image 184
Cameron Castillo Avatar answered Oct 23 '22 03:10

Cameron Castillo


Actually, RAISERROR has been deprecated in favour of THROW since SQL Server 2012. Go here for more information. One of the more amusing aspects is that it is Raiserror and not RaiseError leading to it being called "raise ror" in some circles.

Sample from BOL:

USE tempdb;
GO
CREATE TABLE dbo.TestRethrow
(    ID INT PRIMARY KEY
);
BEGIN TRY
    INSERT dbo.TestRethrow(ID) VALUES(1);
--  Force error 2627, Violation of PRIMARY KEY constraint to be raised.
    INSERT dbo.TestRethrow(ID) VALUES(1);
END TRY
BEGIN CATCH

    PRINT 'In catch block.';
    THROW;
END CATCH;
like image 38
MarkD Avatar answered Oct 23 '22 02:10

MarkD


Use the s% wild card so that you can pass in any message you like from any of your stored procs:

IF NOT EXISTS (SELECT * FROM sys.messages m WHERE m.message_id = 62000)
EXEC sys.sp_addmessage @msgnum = 62000, 
                       @severity = 16, 
                       @msgtext = N'%s', 
                       @lang = 'us_english'

Then in your sp you can raise the error like this:

RAISERROR (62000, 16, 1, 'Error and/or Business Error Text goes here')
like image 27
mark Avatar answered Oct 23 '22 02:10

mark