How do you use the RAISEERROR function to generate custom error messages in SQL Server?
Posted by BobHarris
Last Updated: June 26, 2024
The RAISEERROR function in SQL Server is used to generate error messages and can be very useful for custom error handling in your T-SQL code. It allows you to create errors with specific messages, severity levels, and states. Here's how you can use it effectively:
Syntax
RAISEERROR (message_string, severity, state)
- message_string: The custom error message you want to raise. This can be a string literal or a message from the sys.messages catalog. - severity: An integer from 0 to 25 that indicates the type of error. Severity levels from 0 to 10 are informational; levels from 11 to 16 indicate errors that can be corrected by the user. Levels from 17 to 25 indicate system-level errors. - state: An integer from 0 to 255 that indicates the state or location of the error. This is typically used for debugging.
Example 1: Basic Usage
BEGIN TRY
    -- Some operation that can cause an error
    DECLARE @Value INT = 1;
    IF @Value = 1
        RAISEERROR ('This is a custom error message', 16, 1);
END TRY
BEGIN CATCH
    -- Handle the error
    SELECT ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
In this example, if @Value equals 1, a custom error message is raised, and the CATCH block will capture and display the error message.
Example 2: Using with Variables
DECLARE @CustomErrorMessage NVARCHAR(255);
SET @CustomErrorMessage = 'An error has occurred with ID: 1001';

BEGIN TRY
    -- Raise a custom error
    RAISEERROR (@CustomErrorMessage, 16, 1);
END TRY
BEGIN CATCH
    SELECT ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
This example sets a custom error message in a variable and raises it, which is then caught and displayed in the CATCH block.
Example 3: Using Error Number from sys.messages
You can create a user-defined error message in SQL Server that can be raised later with RAISEERROR. For this, you can use the sp_addmessage stored procedure:
EXEC sp_addmessage @msgnum = 50001, 
                   @msgtext = 'Custom error occurred: %s', 
                   @lang = 'us_english';

BEGIN TRY
    -- Raise the error using the custom message
    DECLARE @ErrorDetail NVARCHAR(100) = 'Invalid operation';
    RAISERROR(50001, 16, 1, @ErrorDetail);
END TRY
BEGIN CATCH
    SELECT ERROR_MESSAGE() AS ErrorMessage;
END CATCH;

-- Optionally remove the message if no longer needed
EXEC sp_dropmessage @msgnum = 50001, @lang = 'us_english';
In this example, a custom message is added and raised later, allowing for more dynamic error handling based on specified conditions.
Important Considerations
- Always ensure that the severity level you use fits the nature of the error. - RAISEERROR does not terminate the batch; it merely generates the error event. - Combined with TRY...CATCH blocks, you can implement robust error handling mechanisms in your SQL Server applications.
Conclusion
The RAISEERROR function is a powerful tool for creating custom error messages in SQL Server, and it can greatly aid in maintaining clarity and control in your data handling and application logic.
Related Content