MySQL improves the buffer for reading abnormal errors (MySQL5.6)

Source: Internet
Author: User

MySQL improves the buffer for reading abnormal errors (MySQL5.6)

What is an error buffer?

In MySQL, the Error Buffer only records the last error. If a new error is generated, the old one will be overwritten. If you want to know what errors are generated, you must follow "show warnings" or "show errors" after each statement that may have errors ". The simplest way is to redirect the output result to the custom log file, so that it can be conveniently viewed later. If you want to view these errors at any time, you must use the APIS provided by MySQL.

Since the emergence of the revolutionary MySQL 5.6 version, the problem has been preliminarily solved. Although it is far from what we think.

MySQL 5.6 provides the get diagnostic statement to get the content of the Error Buffer, and then output the content to variables in different range fields, so that we can flexibly perform subsequent operations.

Next, let's look at an example.

The target table structure is:

  1. CREATE TABLE'T_ datetime '(
  2. 'Id'Int(11)NOT NULL,
  3. 'Log _ time'Timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  4. 'End _ time' datetimeNOT NULL,
  5. PRIMARY KEY('Id ')
  6. ) ENGINE = InnoDBDEFAULTCHARSET = utf8
The Stored Procedure Code is:
  1. DELIMITER $
  2. USE 'new _ t' $
  3. DROP PROCEDUREIf exists 'SP _ do_update '$
  4. CREATEDEFINER = 'root' @ 'localhost'PROCEDURE'SP _ do_update '(
  5. INF_idINT,
  6. INF_log_timeVARCHAR(255 ),
  7. INF_end_timeVARCHAR(255)
  8. )
  9. BEGIN
  10. DECLAREI _con1 TINYINTDEFAULT0;
  11. DECLAREI _codeCHAR(5)DEFAULT '123';
  12. DECLAREI _msg TEXT;
  13. DECLAREI _rowsINT;
  14. DECLAREI _con1 CONDITIONFOR1048;-- This error code indicates that the field cannot be NULL.
  15. DECLARE CONTINUEHANDLERFORI _con1
  16. BEGIN
  17. SETI _con1 = 1;
  18. Get diagnostics CONDITION 1
  19. I _code = returned_sqlstate, I _msg = message_text;
  20. END;
  21. UPDATET_datetime
  22. SETLog_time = IFNULL (f_log_time,NULL),
  23. End_time = IFNULL (f_end_time,NULL)
  24. WHEREId = f_id;
  25. IF I _con1 = 0THEN
  26. Get diagnostics I _rows = ROW_COUNT;
  27. SET@ I _result = CONCAT ("Update succeeded, affected", I _rows,'.');
  28. ELSE
  29. SET@ I _result = CONCAT ('Update failed, error code is 1042, related message is', I _msg,'.');
  30. ENDIF;
  31. SELECT@ I _result;
  32. END$
  33. DELIMITER;

After the above stored procedure is completed, the error information can be saved to the SESSION variable @ I _result. This facilitates the subsequent output.

  1. CALL sp_do_update (1, NOW (), DATE_ADD (NOW (), INTERVAL 1DAY));
  2. Result.
  3. UpdateSucceeded, affected 1.
  4. CALL sp_do_update (1,NULL,NULL);
  5. Result.
  6. UpdateFailed, error codeIs1042, related messageIs Column 'Log _ time'Cannot beNull.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.