RAISERROR in SQL Server

Common SQL Errors

Syntax
Sign up for the latest in common SQL errors from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

RAISERROR is a T-SQL command used to generate custom error messages and manage control flow during query execution or inside stored procedures.

SQL Server

Custom-defined message generated using RAISERROR. Example: Msg 50000, Level 16, State 1, Line 1: Custom error message

Explanation

RAISERROR is used in SQL Server to throw an error message and optionally stop query execution or return control to a calling procedure. It’s especially useful for debugging, validation, and enforcing custom rules in stored procedures or scripts.

The syntax allows you to specify:

  • A message (by ID or custom string)
  • Severity level (from 0 to 25)
  • State (user-defined integer to help track location or context)

You can also include message formatting placeholders (%s, %d, etc.) similar to printf in other languages.

SQL Server processes RAISERROR before TRY...CATCH if the severity is high enough, making it a foundational tool for error management in stored procedures.

Note: RAISERROR is still supported but newer codebases may prefer THROW (introduced in SQL Server 2012), which has simpler syntax and better integration with TRY...CATCH.

Common Causes

  • Wanting to return a user-friendly or custom error from a stored procedure
  • Debugging execution paths with intermediate errors
  • Intentionally halting execution on invalid input or logic
  • Using error messages stored in sys.messages for localization or reuse
  • Related Errors

  • THROW (T-SQL error handling)
  • TRY...CATCH blocks
  • sys.messages table
  • SQL Server error severity levels
  • FAQs

    Q: What’s the difference between RAISERROR and THROW?
    A: THROW is newer, cleaner, and works better with TRY...CATCH. But RAISERROR offers more control over formatting, severity, and use of custom message IDs.

    Q: What does severity mean in RAISERROR?
    A: It indicates how serious the error is. Severity 10 is informational, 16 is a general user error, and anything above 19 requires elevated privileges.

    Q: Can I stop execution with RAISERROR?
    A: Yes—raising an error with a severity of 16 or higher inside a TRY block will jump to the CATCH block. Without error handling, it will stop execution.

    Check out some other errors

    Trusted by top engineers on high-velocity teams
    Aryeo Logo
    Assort Health
    Curri
    Rubie
    Comulate
    Truvideo Logo