13

How can I get error text in MySQL stored procedure when SQLEXCEPTION occurs?

I need something like this:

DECLARE EXIT HANDLER FOR SQLEXCEPTION    
BEGIN
    INSERT INTO my_log (error_message) 
    VALUES (SQLEXCEPTION.message);
END;
5
  • Which version of MySql are you using?
    – Nodlehs
    Oct 24, 2011 at 16:31
  • 5.1.49 I found that not long ago was added new feature GET DIAGNOSTICS, that solves my problem.
    – shurik2533
    Oct 25, 2011 at 5:32
  • But it is added to 5.6.4... anyway thanks for the answer!
    – miherrma
    Oct 25, 2011 at 6:02
  • GET DIAGNOSTICS is available from 5.6.4. Is there another way for lower version,.?
    – Praditha
    Sep 6, 2012 at 6:32

1 Answer 1

8
DECLARE EXIT HANDLER FOR SQLEXCEPTION    
BEGIN

 GET DIAGNOSTICS CONDITION 1
    @p2 = MESSAGE_TEXT;

    INSERT INTO my_log (error_message) 
    SELECT @p2;
END;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.