Stored Procedure – RAISERROR

Here’s an example of how to make a user-defined error message and raise it in the event that something goes wrong.

— Creating the procedure
CREATE PROCEDURE uspUpdateValue
AS

BEGIN TRY

DECLARE @ErrorMsg VARCHAR(100)
DECLARE @ErrorMessage VARCHAR(100)
DECLARE @Severity INT
DECLARE @State INT
SET @Severity = 10
SET @State = 1
SET @ErrorMessage = ‘An error occured while updating Table Order Summary’
UPDATE tbOrderSummary SET tiAgentID = ‘Something‘ WHERE iOrderID = 12828
END TRY

BEGIN CATCH
RAISERROR(@ErrorMessage, @Severity, @State)
END CATCH;
GO

— Execute the stored procedure
EXEC uspUpdateValue 

— Output
Error Returned as specified
(0 rows affected)
An error occured while updating Table Order Summary

Happy learning..!!!

Leave a Reply