RAISERROR is a T-SQL command used to generate custom error messages and manage control flow during query execution or inside stored procedures.
Custom-defined message generated using RAISERROR. Example: Msg 50000, Level 16, State 1, Line 1: Custom error message
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:
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
.
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.