【发布时间】:2011-12-07 13:42:12
【问题描述】:
我相信目前 MySQL 中没有任何可用的东西允许访问 MySQL 存储过程中最后执行的语句的SQLSTATE。这意味着当在存储过程中引发泛型 SQLException 时,很难/不可能得出错误的确切性质。
是否有人有解决方法来导出 MySQL 存储过程中的错误SQLSTATE,而不涉及为每个可能的 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
【问题讨论】:
标签: mysql stored-procedures error-handling