MySQL Error Handling – How to Display Error Messages

error handlingMySQLmysql-5.5

I need to get the whole error message returned from MySQL and assign it to @error_string :

DECLARE CONTINUE HANDLER FOR SQLEXCEPTION   
       BEGIN 
        SET @error_string = 'SQLEXCEPTION_ERROR_MESSAGE'; <-- here
       END;

Is there any alternative, thanks.

Best Answer

From MySQL manual:

DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
    BEGIN
      GET DIAGNOSTICS CONDITION 1
        code = RETURNED_SQLSTATE, error_string = MESSAGE_TEXT;
    END;

I am just not sure about the use of local vs. user variables. User ones are visible for entire session and written in the @var form and if you need them, try to just adding @ to the var names in that statement.