RAISEERROR In SQL?

Forums SQLRAISEERROR In SQL?
Staff asked 10 months ago

What is the use of RAISEERROR? 

can anyone explain it with the help of a small example?

 

Answers (1)

Add Answer
Staff answered 8 months ago

RAISERROR

Creates an error message and starts the session’s error processing. RAISERROR can create a message dynamically or reference a user-defined message saved in the sys.messages catalog view. The message is returned to the caller program as a server error message or to a TRY…CATCH construct’s associated CATCH block.

In other, you can raise a custom error message with this keyword.

The example is shown below. Here I checking if the number is less than zero then raise an error.

BEGIN TRY

    DECLARE @NUM AS INT
    SET @NUM = 2 - 5
    
    IF @NUM < 0
    PRINT(@NUM)
    RAISERROR('Number Cannot be Negative',1,11)
    
END TRY
BEGIN CATCH
  
END CATCH

 

Subscribe

Select Categories