小编典典

MySQL存储过程错误处理

mysql

我相信MySQL当前没有可用的东西允许访问SQLSTATEMySQL存储过程中最后执行的语句。这意味着SQLException在存储过程中引发泛型时,很难/不可能得出错误的确切性质。

是否有人有变通办法来推导SQLSTATEMySQL存储过程中的错误,而不涉及为每个可能的SQLSTATE声明处理程序?

例如,假设我正在尝试返回一个error_status,它超出了下面的通用“ SQLException在此BEGIN....END块中的某处发生”:

DELIMITER $$

CREATE PROCEDURE `myProcedure`(OUT o_error_status varchar(50))
MY_BLOCK: BEGIN

 DECLARE EXIT handler for 1062 set o_error_status := "Duplicate entry in table";
 DECLARE EXIT handler for 1048 set o_error_status := "Trying to populate a non-null column with null value"; 
-- declare handlers ad nauseum here....

 DECLARE EXIT handler for sqlexception set o_error_status:= "Generic SQLException. You'll just have to figure out the SQLSTATE yourself...." ;

-- Procedure logic that might error to follow here...

END MY_BLOCK$$

有小费吗?

PS我正在运行MySQL 5.1.49


阅读 259

收藏
2020-05-17

共1个答案

小编典典

GET DIAGNOSTICS在5.6.4中可用

http://dev.mysql.com/doc/refman/5.6/en/get-
diagnostics.html

2020-05-17