What does Raiserror do in SQL Server? Can someone tell me with the example?
Loading
What does Raiserror do in SQL Server? Can someone tell me with the example?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 31, 2023, 6:28 AM EST
Hi Aradhana,
The
RAISERRORstatement allows you to generate your own error messages and return these messages back to the application using the same format as a system error or warning message generated by SQL Server Database Engine. In addition, theRAISERRORstatement allows you to set a specific message id, level of severity, and state for the error messages.The following illustrates the syntax of the
RAISERRORstatement:RAISERROR ( { message_id | message_text | @local_variable } { ,severity ,state } [ ,argument [ ,...n ] ] ) [ WITH option [ ,...n ] ];examples
DECLARE @ErrorMessage NVARCHAR(4000), @ErrorSeverity INT, @ErrorState INT; BEGIN TRY RAISERROR('My Custome exception message.', 17, 1); END TRY BEGIN CATCH SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); -- return the error inside the CATCH block RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState); END CATCH;Here is the output:
Msg 50000, Level 17, State 1, Line 16 My Custome exception message.For more details, refer : https://www.sqlservertutorial.net/sql-server-stored-procedures/sql-server-raiserror/
Thanks
Naimish
Sachin SinghRank #66 · 26.2K reputationPosted Jan 31, 2023, 6:39 AM EST
You can follow this article for detailed explaination
https://www.sqlservertutorial.net/sql-server-stored-procedures/sql-server-raiserror/
Vishal YelveRank #178 · 13.6K reputationPosted Jan 31, 2023, 8:26 AM EST
RAISERRORcan either reference a user-defined message stored in thesys.messagescatalog view, or build a message dynamically. The message is returned as a server error message to the calling application or to an associatedCATCHblock of aTRY...CATCHconstruct.do refer below link
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/raiserror-transact-sql?view=sql-server-ver16
Aravind GovindarajRank #430 · 5.8K reputationPosted Jan 31, 2023, 9:40 AM EST
On a high level, it's used to get messages from the backend to the front-end application or call a spot. It may be information or error based on how you use the raise error
Rajeev KumarRank #1,334 · 1.3K reputationPosted Mar 2, 2023, 8:49 AM EST
The
RAISERRORstatement allows you to generate your own error messages and return these messages back to the application using the same format as a system error or warning message generated by SQL Server Database Engine. In addition, theRAISERRORstatement allows you to set a specific message id, level of severity, and state for the error messages.DECLARE
@ErrorMessage NVARCHAR(4000),
@ErrorSeverity INT,
@ErrorState INT;
BEGIN TRY
RAISERROR('Error occurred in the TRY block.', 17, 1);
END TRY
BEGIN CATCH
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
-- return the error inside the CATCH block
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
END CATCH;