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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Naimish MakwanaPosted Jan 31, 2023, 11:28 AM
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:examples
Here is the output:
For more details, refer : https://www.sqlservertutorial.net/sql-server-stored-procedures/sql-server-raiserror/
Thanks
Naimish
Rajeev KumarPosted Mar 2, 2023, 1:49 PM
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;
Aravind GovindarajPosted Jan 31, 2023, 2:40 PM
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
Vishal YelvePosted Jan 31, 2023, 1:26 PM
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
Sachin SinghPosted Jan 31, 2023, 11:39 AM
You can follow this article for detailed explaination
https://www.sqlservertutorial.net/sql-server-stored-procedures/sql-server-raiserror/