据我所知,目前在MySQL中没有任何可用的方法来访问MySQL存储过程中最后一个执行语句的SQLSTATE
。这意味着,当存储过程中引发通用的SQLException
时,很难/不可能确定错误的确切性质。
有没有人有一种方法可以推导出MySQL存储过程中错误的SQLSTATE
,而不涉及为每个可能的SQLSTATE
声明处理程序的解决方法?
例如-假设我正在尝试在以下代码块中返回超出常规“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
SHOW ERRORS
的结果存储到一个变量中? - AAEM