mysqlsqldatabaseraiseerror

How to Handle the RAISERROR in SQL Database


I want to handle the below Error into a variable, so that I can store it in a table

RAISERROR ('Error occurred "%s" after 10 passes.', 1, 1, @Requesterr) 

I need to get whole error from above command and store it in table.

Thanks in advance..!!


Solution

  • You can use FORMATMESSAGE to format error message:

    DECLARE @error_msg VARCHAR(100);
    DECLARE @Requesterr VARCHAR(100) = 'incorrect path';
    
    SET @error_msg = FORMATMESSAGE('Error occurred "%s" after 10 passes.', @Requesterr);
    
    RAISERROR (@error_msg, 1, 1);
    
    -- INSERT INTO error_log(message)
    -- VALUE (@error_msg);
    

    LiveDemo