14

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? Commented Oct 24, 2011 at 16:31
  • marcalff.blogspot.com/2011/10/mysql-get-diagnostics.html Commented Oct 25, 2011 at 5:24
  • 5.1.49 I found that not long ago was added new feature GET DIAGNOSTICS, that solves my problem. Commented Oct 25, 2011 at 5:32
  • But it is added to 5.6.4... anyway thanks for the answer! Commented Oct 25, 2011 at 6:02
  • GET DIAGNOSTICS is available from 5.6.4. Is there another way for lower version,.? Commented Sep 6, 2012 at 6:32

1 Answer 1

11
DECLARE EXIT HANDLER FOR SQLEXCEPTION    
BEGIN

 GET DIAGNOSTICS CONDITION 1
    @p2 = MESSAGE_TEXT;

    INSERT INTO my_log (error_message) 
    SELECT @p2;
END;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.